У меня есть этот блок кода, который ведет себя так, как я хочу:
// version with CountDownLatch
public static void myCountDownLatch() {
CountDownLatch countDownLatch = new CountDownLatch(1);
Thread t = new Thread(() ->
{
try {
log.info("CountDownLatch: in thread..");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
});
t.start();
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("CountDownLatch: out thread..");
}
Но при использовании Phaser
вместо основного потока не , ожидая, пока Thread t
не будет законченный.
Что я должен изменить в своем Phaser
коде, чтобы он вел себя так же, как и в приведенной выше версии с CountDownLatch
?
// version with Phaser
public static void myPhaser() {
Phaser phaser = new Phaser(1);
Thread t = new Thread(() ->
{
try {
log.info("phaser: in thread..");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
phaser.arriveAndDeregister();
});
t.start();
phaser.arriveAndAwaitAdvance();
log.info("phaser: out thread..");
}