当前位置:网站首页>[HBZ sharing] principle of semaphore and countdownlatch
[HBZ sharing] principle of semaphore and countdownlatch
2022-06-29 11:37:00 【hbz-】
principle :
Both are based on AQS Realized
Semaphore Introduction to semaphore principle and function
- utilize Semaphore The semaphore can be used as a current limiting interface , That is, after configuring the maximum semaphore .
- Every call acquire() Method , The semaphore will -1, When it comes to 0 when , In the call of the acquire The thread will enter the blocking state
- Every call release() Method , The semaphore will +1, At this point, the thread in the blocking queue will be awakened for execution , And make the semaphore -1, Until the blocking queue is empty
Semaphore Semaphore API And usage
// Create semaphore object , And the maximum semaphore is given 5 individual
Semaphore semaphore = new Semaphore(5);
for(int i =0; i < 10; i++){
// Acquisition semaphore , And count the two remaining signals - 1
semaphore.acquire();
System.out.println(" Perform logical ");
// Return the semaphore
semaphore.release();
}
CountDownLatch Principle and function introduction
- Block the current thread
- CountDownLatch countDownLatch = new CountDownLatch(2); It means that every time countDownLatch.await(), It needs to be executed twice countDownLatch.countDown(); To continue executing the current thread
CountDownLatch Of API And usage
CountDownLatch countDownLatch = new CountDownLatch(2);
new Thread(()->{
System.out.println(" Thread start ");
// Block the thread
countDownLatch.await();
System.out.println(" Thread end ");
}).start();
// For the first time , It does not release the lock , because new CountDownLatch(2); When parameters are 2, No printing at this time 【 Thread end 】
countDownLatct.countDown();
// Carry out the second , Can be completely released , And then , Print 【 Thread end 】
countDownLatct.countDown();
边栏推荐
- Mastering the clever use of some shell wildcards will make us write with half the effort
- Qt学习10 初探 Qt 中的消息处理
- Spark - Task 与 Partition 一一对应与参数详解
- Modbustcp protocol WiFi wireless learning single channel infrared module (small shell version)
- The Chinese Computational Linguistics Conference and the national knowledge atlas and Semantic Computing Conference are in full swing
- Modbustcp protocol network learning single channel infrared module (double-layer board)
- Qt学习08 启航!第一个应用实例
- Shell quotation marks and escape are rarely noticed, but they are often used in writing scripts
- Online text filter less than specified length tool
- Spark - one to one correspondence between task and partition and detailed explanation of parameters
猜你喜欢

Xuetong denies that the theft of QQ number is related to it: it has been reported; IPhone 14 is ready for mass production: four models are launched simultaneously; Simple and elegant software has long

Qt学习05 Qt Creator工程介绍

Specific method and example program of Siemens s7-200smart control stepping motor
![[NLP] text generation topic 1: basic knowledge](/img/cf/2e29e55344ea4cc8ce48777878b867.png)
[NLP] text generation topic 1: basic knowledge

软件工程导论——第五章——总体设计

多线程高并发服务器:3个问题

Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)

Bs-gx-017 online examination management system based on SSM

How to obtain method parameter values through WinDbg
![[digital signal modulation] realize signal modulation and demodulation based on am+fm+dsb+ssb, including Matlab source code](/img/76/bcf0118c8eea2b45b47eda4a68d3fd.png)
[digital signal modulation] realize signal modulation and demodulation based on am+fm+dsb+ssb, including Matlab source code
随机推荐
又拍云 Redis 的改进之路
What are the pop, push, unshift, and shift of the (JS) array?
稳定币风险状况:USDT 和 USDC 安全吗?
Mastering the clever use of some shell wildcards will make us write with half the effort
又拍雲 Redis 的改進之路
Go 单元测试入门实践
Opencv4.0 installation of raspberry pie for Xiaobai
[NLP] text generation topic 1: basic knowledge
重建中国科研自信——2022最新自然指数排行榜(Nature Index 2022 )公布,中国的研究产出增幅最大...
Necessary for cloud native development: the first common codeless development platform IVX editor
Qt学习05 Qt Creator工程介绍
工具箱之 IKVM.NET 项目新进展
Specific method and example program of Siemens s7-200smart control stepping motor
5. migrate uboot set default environment variables, crop, and partition
2.8万字的Callable和Future面试知识点总结,看完我直接面进了字节跳动,原谅我有点飘了(下)
斐波那锲数列与冒泡排序法在C语言中的用法
Ikvm Net project progress
专访 SUSS NiFT 负责人:Web3 的未来离不开“人人为我,我为人人”的治理
The Chinese Computational Linguistics Conference and the national knowledge atlas and Semantic Computing Conference are in full swing
【无标题】我在密谋一件大事