import lombok.SneakyThrows;
import java.util.Date;
import java.util.concurrent.*;
/*
main上鎖3
執行緒1獲得鎖
執行緒2獲得鎖
執行緒3獲得鎖
執行緒1釋放鎖2
執行緒2釋放鎖1
執行緒3釋放鎖0
main解鎖0
* */
public class T {
@SneakyThrows
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(3);
System.out.println(Thread.currentThread().getName() + "上鎖" + latch.getCount());
new Thread(() -> {
try {
System.out.println(Thread.currentThread().getName() + "獲得鎖");
Thread.sleep(200);
latch.countDown();
System.out.println(Thread.currentThread().getName() + "釋放鎖" + latch.getCount());
} catch (Exception e) {
}
}, "執行緒1").start();
new Thread(() -> {
try {
System.out.println(Thread.currentThread().getName() + "獲得鎖");
Thread.sleep(500);
latch.countDown();
System.out.println(Thread.currentThread().getName() + "釋放鎖" + latch.getCount());
} catch (Exception e) {
}
}, "執行緒2").start();
new Thread(() -> {
try {
System.out.println(Thread.currentThread().getName() + "獲得鎖");
Thread.sleep(1000);
latch.countDown();
System.out.println(Thread.currentThread().getName() + "釋放鎖" + latch.getCount());
} catch (Exception e) {
}
}, "執行緒3").start();
latch.await();
System.out.println(Thread.currentThread().getName() + "解鎖" + latch.getCount());
}
}
CountDownLatch
相關文章
- CyclicBarrier And CountDownLatchCountDownLatch
- 詳解CountDownLatchCountDownLatch
- 手寫CountDownLatchCountDownLatch
- 同步工具類—— CountDownLatchCountDownLatch
- CountDownLatch原始碼解析CountDownLatch原始碼
- java原始碼-CountDownLatchJava原始碼CountDownLatch
- JUC:05.CountDownLatchCountDownLatch
- CountDownLatch原始碼分析CountDownLatch原始碼
- java CountDownLatch 詳解JavaCountDownLatch
- CountDownLatch原始碼閱讀CountDownLatch原始碼
- JUC之CountDownLatch原始碼分析CountDownLatch原始碼
- JUC(3)---CountDownLatch、CyclicBarrier和AQSCountDownLatchAQS
- Java併發—— CountDownLatch與CyclicBarrierJavaCountDownLatch
- CountDownLatch 概述和原始碼分析CountDownLatch原始碼
- java 多執行緒CountDownLatchJava執行緒CountDownLatch
- 【java】CountDownLatch運用場景(1)JavaCountDownLatch
- java多執行緒系列:CountDownLatchJava執行緒CountDownLatch
- 併發程式設計之:CountDownLatch程式設計CountDownLatch
- Java 併發包之CountDownLatch、CyclicBarrierJavaCountDownLatch
- CountDownLatch和CyclicBarrier的簡單使用CountDownLatch
- 倒數計時門栓(CountDownLatch)CountDownLatch
- CyclicBarrier、CountDownLatch與Semaphore的小記CountDownLatch
- java 多執行緒 CountDownLatch用法Java執行緒CountDownLatch
- 同步輔助類CountDownLatch的例子CountDownLatch
- 漫畫:Java併發閉鎖CountDownLatchJavaCountDownLatch
- CountDownLatch和CyclicBarrier區別及詳解CountDownLatch
- 高併發之ReentrantLock、CountDownLatch、CyclicBarrierReentrantLockCountDownLatch
- Java併發(6)- CountDownLatch、Semaphore與AQSJavaCountDownLatchAQS
- 併發模擬-程式碼CountDownLatch,SemaphoreCountDownLatch
- CountDownLatch、CyclicBarrier、Semaphore、Exchanger 的詳細解析CountDownLatch
- CyclicBarrier、CountDownLatch以及Semaphore使用及其原理分析CountDownLatch
- Java併發工具類(閉鎖CountDownLatch)JavaCountDownLatch
- Java併發之CountDownLatch、CyclicBarrier和SemaphoreJavaCountDownLatch
- Java 併發工具類 CountDownLatch、CyclicBarrier、Semaphore、ExchangerJavaCountDownLatch
- 一文搞懂 CountDownLatch 用法和原始碼!CountDownLatch原始碼
- 面試官:說說CountDownLatch,CyclicBarrier,Semaphore的原理?面試CountDownLatch
- Java多執行緒同步工具類之CountDownLatchJava執行緒CountDownLatch
- 【對線面試官】CountDownLatch和CyclicBarrier的區別面試CountDownLatch