在运行每个线程时,为什么即使在前一个线程已经调用 countdown.countDown() 并将 Latch Count 减 1 之后,countdown.getCount() 总是打印“3”?
我有点担心 Java 如何知道 Latch Count 已达到 0,以便它可以释放所有 3 个线程。
import java.util.concurrent.CountDownLatch;
class b {
static final CountDownLatch countdown = new CountDownLatch(3);
public static void main(String[] args) {
for (int i = 0; i < 3; ++i) {
Thread t = new Thread() {
public void run() {
System.out.printf("Starting on %d other threads.\n",
countdown.getCount());
countdown.countDown();
System.out.printf("new on %d other threads.\n",
countdown.getCount());
try {
countdown.await(); // waits until everyone reaches this
// point
// System.out.println("Go again : "
// +countdown.getCount());
} catch (Exception e) {
}
}
};
t.start();
}
System.out.println("Go");
}
}