It is for this that there are barriers, you can implement this using CyclicBarrier or CountDownLatch . These are the synchronizers used to delay the progress of the threads until they reach the desired state, in your case, the threads completed the calculation.
Here it depends on how you want to implement:
; .
CyclicBarrier, :
this.barrier = new CyclicBarrier(count);
Runnable : barrier.await()
public class Slaves implements Runnable {
@Override
public void run() {
while(condition) {
try {
barrier.await();
} catch (InterruptedException ex) {
return;
} catch (BrokenBarrierException ex) {
return;
}
}
}
}
, . , .
, , , , ( ), Runnable CyclicBarrier, , .
this.barrier = new CyclicBarrier(count,
new Runnable() {
@Override
public void run() {
}
}
);