Java CyclicBarrier:修订间差异
建立內容為「A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. <span class="article-label">CyclicBarriers</span> a…」的新頁面 |
小 Riguz移动页面Java:CyclicBarrier至Java CyclicBarrier,不留重定向 |
||
(没有差异)
|
2023年12月19日 (二) 06:53的最新版本
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released.
class Solver {
final int N;
final float[][] data;
final CyclicBarrier barrier;
class Worker implements Runnable {
int myRow;
Worker(int row) { myRow = row; }
public void run() {
while (!done()) {
processRow(myRow);
try {
barrier.await();
} catch (InterruptedException ex) {
return;
} catch (BrokenBarrierException ex) {
return;
}
}
}
}
public Solver(float[][] matrix) {
data = matrix;
N = matrix.length;
barrier = new CyclicBarrier(N,
new Runnable() {
public void run() {
mergeRows(...);
}
});
for (int i = 0; i < N; ++i)
new Thread(new Worker(i)).start();
waitUntilDone();
}
}