java多執行緒 CountDownLatch減法計數器
import java.util.concurrent.CountDownLatch;
public class CountDownLatchDemo {
public static void main(String[] args) {
CountDownLatch countDownLatch = new CountDownLatch(6);
for (int i = 1; i <= 6; i++) {
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + " Go out");
countDownLatch.countDown();// 數量 -1
}, String.valueOf(i)).start();
}
try {
countDownLatch.await();// 等等計數器歸零,然后程式向下執行
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("close door");
}
}
運行結果
3 Go out
6 Go out
5 Go out
4 Go out
1 Go out
2 Go out
close door
特別鳴謝:狂神說Java
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/287098.html
標籤:其他
下一篇:Day296.原子類 -Juc
