Есть несколько проблем.
1) Вы не создаете темы. Вы можете создавать темы из Runnable следующим образом:
Thread t = new Thread(runnable); //create thread
t.start(); //start the thread
измени свой код:
for(int i=0; i<length; i++)
cells[i].run();
Примерно так:
for (int i = 0; i < length; i++)
new Thread(cells[i]).start();
2) Вы не печатаете массив после каждого цикла, вы фактически не реализуете цикл, чтобы иметь цикл. Чтобы напечатать массив после каждого цикла, создайте новый Runnable, который будет вызываться, когда все потоки достигнут циклического барьера, вы можете напрямую установить этот Runnable в циклический барьер
ТАК измените свой код:
Scanner stdin = new Scanner(System.in);
length = stdin.nextInt();
amountOfCycles = stdin.nextInt();
barrier = new CyclicBarrier(length);
cells = new Cell[length];
for(int i=0; i<length; i++)
cells[i] = new Cell(i);
Примерно так:
Scanner stdin = new Scanner(System.in);
length = stdin.nextInt();
amountOfCycles = stdin.nextInt();
cells = new Cell[length];
for (int i = 0; i < length; i++)
cells[i] = new Cell(i);
barrier = new CyclicBarrier(length, () -> {
System.out.println(Arrays.toString(cells)); //code that will run every time when all thread reach the cyclic barrier
});
3) Создать цикл в темах:
Измените свой код:
try{
// Wait for the start of the cycle:
barrier.wait(); //remove this, you never called notify so its useless
//business logic omitted
// Wait until every cell is done calculating its new value:
barrier.await();
// And then actually update the values of the cells
value += increment;
}catch(Exception ex){
System.err.println("Exception occurred! " + ex);
ex.printStackTrace();
}
Примерно так:
int cycleCounter = 0;
while (cycleCounter < amountOfCycles) {
cycleCounter++;
try {
//business logic omitted
barrier.await();
// And then actually update the values of the cells
value += increment;
} catch (Exception ex) {
System.err.println("Exception occurred! " + ex);
ex.printStackTrace();
}
}