当前位置:网站首页>About the three commonly used auxiliary classes of JUC
About the three commonly used auxiliary classes of JUC
2022-04-23 10:57:00 【Chunni】
1.CountDownLatch
First, let's look at the help document

From the help document, we can see CountDownLatch It's actually a subtraction counter , adopt await Methods block , Until the count is reduced to 0 after , Other threads can execute .
Through a little demo try
public class CountDownLatchDemo {
public static void main(String[] args) throws Exception {
// The total number is 6, Use it when you have to perform a task
CountDownLatch countDownLatch = new CountDownLatch(6);
for (int i = 1; i <= 6; i++) {
int temp = i;
new Thread(()->{
System.out.println(Thread.currentThread().getName() + "=> Here comes the guest ");
countDownLatch.countDown();//-1
},String.valueOf(i)).start();
}
countDownLatch.await();// Wait for the counter to zero , Then go down
System.out.println(" The guests are here , Serve ");
}
}
Every time a thread calls , Is executed countDown() Make the quantity -1, Until the number of counters is 0,await() Will be awakened , Continue with the rest of the code
2.CyclicBarrier
In fact, the common barrier here is an addition counter , Each thread uses await Methods block , Release after reaching the barrier point .
public class CyclicBarrierDemo {
public static void main(String[] args) {
// After reaching the common barrier , To perform
CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
System.out.println(" The staff is full ");
});
for (int i = 1; i <= 7; i++) {
final int temp = i;
new Thread(()->{
System.out.println(Thread.currentThread().getName() + " recruit " + temp + " Employees ");
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}).start();
}
}
}
3.Semaphore
You can know from the help document ,Semaphore It's a semaphore , Give a set of licenses , When the license is blocked, it is not available , The license will be obtained again after release . And it is used to limit the number of threads . There's a reason for grabbing a parking space here , The total number of cars in the parking lot is limited . Wait without parking space , You can park when you have a parking space .
Take a little demo
public class SemaphoreDemo {
public static void main(String[] args) {
// Number of threads , Applied to current limiting
Semaphore semaphore = new Semaphore(3);
for (int i = 1; i < 7; i++) {
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(3);
semaphore.acquire();
System.out.println(Thread.currentThread().getName()+" Got it. ");
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+" I am leaving ");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
},String.valueOf(i)).start();
}
}
}
principle :
semaphore.acquire() Acquisition semaphore , Suppose it's full , Just wait , Wait until released
semaphore.release() Release semaphore , Then wake up the waiting thread
effect : Mutually exclusive use of multiple shared resources , Concurrent current limiting
版权声明
本文为[Chunni]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231050391610.html
边栏推荐
- Learning notes 7-depth neural network optimization
- 【leetcode】107.二叉树的层序遍历II
- Typora operation skill description (I) md
- Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
- Resolution and size of mainstream mobile phones
- MBA-day5數學-應用題-工程問題
- Source insight 4.0 FAQs
- 主流手机分辨率与尺寸
- SWAT—Samba WEB管理工具介绍
- SQL Server 递归查询上下级
猜你喜欢

Comparison and practice of prototype design of knowledge service app
![[provincial election joint examination 2022 d2t1] card (state compression DP, FWT convolution)](/img/e4/3c47edbc3241ba86f10a1ac8a963fd.png)
[provincial election joint examination 2022 d2t1] card (state compression DP, FWT convolution)

Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)

Learning Notes 6 - Summary of several deep learning convolutional neural networks

Detailed explanation of typora Grammar (I)

UEditor之——图片上传组件大小4M的限制

得到知识服务app原型设计比较与实践

Solutions to common problems in visualization (IX) background color

Comparison and practice of prototype design of knowledge service app

关于JUC三大常用辅助类
随机推荐
SQL Server recursive query of superior and subordinate
第六站神京门户-------手机号码的转换
Embedded related surface (I)
19. Delete the penultimate node of the linked list (linked list)
Full stack cross compilation x86 completion process experience sharing
142. Circular linked list||
学习网站资料
MBA - day5 mathématiques - Questions d'application - Questions d'ingénierie
Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis
Deploy jar package
Contact between domain name and IP address
Visualized common drawing (II) line chart
主流手机分辨率与尺寸
Source insight 4.0 FAQs
微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
Manjaro installation and configuration (vscode, wechat, beautification, input method)
Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
Using El popconfirm and El backtop does not take effect
Visual Road (XII) detailed explanation of collection class
【leetcode】199.二叉树的右视图