I need 3 parameters in my method run. Two of them need to be part of a shared matrix (2 rows of it) and the last one is the master's for index. How can I pass those to my method run? I got quite a huge matrix, so maybe isn't the best idea to duplicate variables in different places... Should I make another class with those attributes, and make this class implement Runnable?
The control ArrayList is intended to work as a place to see if the column in which I'm working is still not the same as the column in the row above as it wouldn't generate a correct answer.
How can I keep them at a maximum determined by threads, and not making my main thread stop? Joining a thread would make my mainThread to stop as far as I know.
My code looks like this:
public class MochilaCeroUno extends Thread {
protected List<Item> itemList = new ArrayList<Item>();
protected int maxWeight = 0;
protected int solutionWeight = 0;
protected int profit = 0;
protected boolean calculated = false;
static int threads;
static List<MyPair> control;
Run method:
public void run(List<Integer> filaAnterior, List<Integer> filaActual, int i) {
MyPair pair = new MyPair(i, 0);
control.set((i - 1) % threads, pair);
int numFilaAnterior = i - 1;
if (numFilaAnterior >= 0) {
for (int j = 0; j <= maxWeight; j++) { //Posar concurrencia aqui
if (control.get((numFilaAnterior - 1) % threads).getCol() > j) {
if (j > 0) {
int wH = itemList.get(i - 1).getWeight();
if (wH > j) {
filaActual.add(filaAnterior.get(j));
} else {
filaActual.add(Math.max(filaAnterior.get(j), itemList.get(i - 1).getValue() + filaAnterior.get(j - wH)));
}
} else {
filaActual.add(0);
}
} else {
j--;
yield();
}
}
super.run();
}
}
Method that calls the run and has to create threads:
public List<Item> calcSolution() {
int n = itemList.size();
setInitialStateForCalculation();
if (n > 0 && maxWeight > 0) {
List<List<Integer>> c = new ArrayList<List<Integer>>();
List<Integer> curr = new ArrayList<Integer>();
c.add(curr);
for (int j = 0; j <= maxWeight; j++) {
curr.add(0);
}
for (int i = 1; i <= n; i++) {
List<Integer> prev = curr;
c.add(curr = new ArrayList<Integer>());
new MochilaCeroUno().start();
...