CyclicBarrier のサンプル。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;public class Solver {
public static void main(String args) {
float matrix = new float[10][10];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = (i + 1) * (j + 1);
}
}new Solver(matrix);
}final int N;
final float data;
final CyclicBarrier barrier;
class Worker implements Runnable {
int myRow;
boolean done;Worker(int row) {
myRow = row;
}public void run() {
while (!done()) {
System.out.println("processRow(" + myRow+ ") : " + Thread.currentThread());try {
barrier.await();
} catch (InterruptedException ex) {
return;
} catch (BrokenBarrierException ex) {
return;
}
done = true;
}
}private boolean done() {
return done;
}
}public Solver(float[] matrix) {
data = matrix;
N = matrix.length;
barrier = new CyclicBarrier(N, new Runnable() {
public void run() {
System.out.println("mergeRows() : " + Thread.currentThread());
}
});
for (int i = 0; i < N; ++i)
new Thread(new Worker(i)).start();System.out.println("waitUntilDone() : " + Thread.currentThread());
System.exit(0);
}
}