public class Test {
final int counter = 0; // it may be a different number
int add = 0;
public synchronized void testSync() throws InterruptedException {
add++;
if (add == counter) {
add = 0;
notifyAll();
} else {
while (add > 0) {
wait();
}
}
}
}
如何修复代码以使其工作?我需要使其作为循环屏障工作。
最佳答案
public class Test {
final int counter = 0; // it may be a different number
final CyclicBarrier barrier = new CyclicBarrier(counter);
public void testSync() throws InterruptedException, BrokenBarrierException {
barrier.await();
}
}
关于java - 循环屏障无法实现同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31280779/