Introduction
CyclicBarrier 很久沒用都忘了, 來複習一下..
Description
簡單說就是很多個 thread 去作事情, 作完之後就 await.
CyclicBarrier 等作完的 thread 達到指定的個數後, await 就結束.
Code
public class CyclicBarrierMain {
public static void main(String[] params) throws InterruptedException {
int runner = 5;
CountDownLatch gameOver = new CountDownLatch(runner);
CyclicBarrier b = new CyclicBarrier(runner, () -> {
System.out.println("barrier done");
});
IntStream.range(0,runner).forEach(idx -> {
new Thread() {
@Override
public void run() {
try {
System.out.println("wait " + idx);
b.await();
System.out.println("count down " + idx);
gameOver.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
});
gameOver.await();
System.out.println("game over");
}
}
沒有留言:
張貼留言