0

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();
           ...
 
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Jo4nP4l4u
  • 85
  • 2
  • 9
  • *Should I make another class with those atributes, and make this class implement Runnable?* Yes. And, please use a thread safe version `List`. https://stackoverflow.com/questions/8203864/choosing-the-best-concurrency-list-in-java – xingbin Oct 31 '18 at 03:03
  • The only thread safe list should be the control list right? I'm new using threads and java... – Jo4nP4l4u Oct 31 '18 at 03:12
  • `filaActual` should be thread safe, since you add elements to it in multiple threads. – xingbin Oct 31 '18 at 03:13
  • You might be better converting this code to use the new Streams abstraction in Java 8(?), then let it parallelize the work for you - but I don know what you're trying to compute here, so I don't know how feasible that transformation woul d be... – moilejter Oct 31 '18 at 03:13
  • I'm trying to comupte Knapsack problem using threads "diagonally". So the previousRow cannot be equal or less than the actualRow. – Jo4nP4l4u Oct 31 '18 at 03:17
  • filaActual is a row in wich only one thread is inserting elements in the position p, but another one can be reading the array till p-1. Does it still need to be thread safe? – Jo4nP4l4u Oct 31 '18 at 03:19

0 Answers0