当前位置:网站首页>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
- Source insight 4.0 FAQs
- 精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
- JDBC – PreparedStatement – 如何设置 Null 值?
- 期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
- Visual Road (XII) detailed explanation of collection class
- Latex usage
- Restful、SOAP、RPC、SOA、微服务之间的区别
- Comparison and practice of prototype design of knowledge service app
- VIM + ctags + cscope development environment construction guide
猜你喜欢

Download and installation steps of xshell + xftp

第六站神京门户-------手机号码的转换

Solution architect's small bag - 5 types of architecture diagrams

Visual Road (XII) detailed explanation of collection class

关于JUC三大常用辅助类

使用 PHP PDO ODBC 示例的 Microsoft Access 数据库

Solutions to common problems in visualization (IX) background color

精彩回顾|「源」来如此 第六期 - 开源经济与产业投资

C语言之结构体(进阶篇)

Idea - indexing or scanning files to index every time you start
随机推荐
全栈交叉编译X86完成过程经验分享
Visual common drawing (I) stacking diagram
How can swagger2 custom parameter annotations not be displayed
STM32接电机驱动,杜邦线供电,然后反烧问题
Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
142. Circular linked list||
Xdotool key Wizard
Visual common drawing (V) scatter diagram
SSH利用私钥无密钥连接服务器踩坑实录
软件测试人员,如何优秀的提Bug?
JVM - common parameters
微信小程序中app.js文件、组件、api
我的创作纪念日
CentOS/Linux安装MySQL
Arbitrary file reading vulnerability exploitation Guide
A diary of dishes | 238 Product of arrays other than itself
349. Intersection of two arrays
任意文件读取漏洞 利用指南
Solution architect's small bag - 5 types of architecture diagrams