Le countDownLatch ne sera pas attendu après countDown() jusqu'à zéro. mais le programme est verrouillé . la dernière impression n'est jamais sortie.
public static void main(String[] args) throws Exception{
CountDownLatch countDownLatch = new CountDownLatch(1);
countDownLatch.await();
new Thread(new Runnable() {
@Override
public void run() {
countDownLatch.countDown();
countDownLatch.countDown();
}
}).start();
System.out.println("------should print-------");
}
si la méthode await est dans un thread (pas le thread principal), cela fonctionne. pourquoi ?
public static void main(String[] args) throws Exception {
CountDownLatch countDownLatch = new CountDownLatch(1);
new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
countDownLatch.countDown();
countDownLatch.countDown();
}
}).start();
System.out.println("------should print-------");
}