当前位置:网站首页>[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();
边栏推荐
- (JS) isnan() method determines the reason why undefined is true
- 又拍云 Redis 的改进之路
- math_ Mathematical expression & deformation of equation equation & accumulation of combined operation skills / means
- MySQL get table information
- 9 easy to use JSON processing tools, greatly improving efficiency!
- Spark - one to one correspondence between task and partition and detailed explanation of parameters
- Limit introduction summary
- What is the experience of working in an IT company in Japan?
- Graduation season · advanced technology Er - workers in the workplace
- Opencv4.0 installation of raspberry pie for Xiaobai
猜你喜欢

Micro blog comment architecture design

Multithreaded high concurrency server: three problems

Self-Improvement! Junior college "counter attack" master of Zhejiang University, 3 SCI, and finally become a doctor of Tsinghua University!

Qt学习08 启航!第一个应用实例

How to test the performance of container platform, including stability, expansion efficiency and component performance

Course design for the end of the semester: product sales management system based on SSM

影响LED封装散热主要因素有哪些?

适合小白的树莓派opencv4.0安装

行业分析| 快对讲,楼宇对讲

Qt学习15 用户界面与业务逻辑的分离
随机推荐
Qt学习11 Qt 中的字符串类
Is it safe to open a stock account online
Spark - one to one correspondence between task and partition and detailed explanation of parameters
影响LED封装散热主要因素有哪些?
MySQL query table field information
Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)
9 easy to use JSON processing tools, greatly improving efficiency!
The necessary operation for those big guys to fly 666 inadvertently at the bash command line terminal
TTL serial port learning infrared remote control module can be extended to network control
Modbustcp protocol WiFi wireless learning single channel infrared module (small shell version)
【HBZ分享】Semaphore 与 CountDownLatch原理
Interview questions of Tencent automation software test of CSDN salary increase secret script (including answers)
Qt学习15 用户界面与业务逻辑的分离
When a technician becomes a CEO, what "bugs" should be modified?
巴比特 | 元宇宙每日必读:HTC 宣布推出首款元宇宙手机,售价约2700元人民币,都有哪些新玩法?...
MySQL 索引失效的几种类型以及解决方式
Week 12 experiment -- implementation of VGA protocol based on FPGA
Multithreaded high concurrency server: three problems
什么?漫画居然能免费看全本了,这还不学起来一起做省钱小能手
Pipeline aggregations管道聚合-Sibling-1