当前位置:网站首页>关于JUC三大常用辅助类
关于JUC三大常用辅助类
2022-04-23 10:51:00 【椿尼】
1.CountDownLatch
首先我们来看帮助文档
从帮助文档中我们可以看出CountDownLatch实际上就是一个减法计数器,通过await方法阻塞,直到计数值减到0之后,其他线程才得以执行。
通过一个小demo试试
public class CountDownLatchDemo {
public static void main(String[] args) throws Exception {
//总数是6,必须要执行任务的时候再使用
CountDownLatch countDownLatch = new CountDownLatch(6);
for (int i = 1; i <= 6; i++) {
int temp = i;
new Thread(()->{
System.out.println(Thread.currentThread().getName() + "=> 来客人了");
countDownLatch.countDown();//-1
},String.valueOf(i)).start();
}
countDownLatch.await();//等待计数器归零,然后再向下执行
System.out.println("客人到齐了,上菜");
}
}

每次有线程调用,就执行countDown()使得数量-1,直到计数器数量为0,await()就会被唤醒,继续执行后面的代码
2.CyclicBarrier

其实这里的达到共同屏障点就是一个加法计数器,每个线程使用await方法阻塞,达到屏障点后释放。
public class CyclicBarrierDemo {
public static void main(String[] args) {
//达到共同的屏障之后,才能执行
CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
System.out.println("员工满了");
});
for (int i = 1; i <= 7; i++) {
final int temp = i;
new Thread(()->{
System.out.println(Thread.currentThread().getName() + "招收" + temp + "个员工");
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}).start();
}
}
}

3.Semaphore

从帮助文档中可以得知,Semaphore是一个信号量,给予一组许可证,当许可证被阻塞了就不可用,释放之后会再次获得许可证。而且是拿来限制线程数的。这里和抢车位是一个道理,停车场总的车位数是限定的。没车位就等待,有车位了就可以停车。
举个小demo
public class SemaphoreDemo {
public static void main(String[] args) {
//线程数量,运用于限流
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()+"抢到了");
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+"离开了");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
},String.valueOf(i)).start();
}
}
}

原理:
semaphore.acquire() 获取信号量,假设满了,就等待,等待被释放为止
semaphore.release() 释放信号量,然后唤醒等待的线程
作用:多个共享资源互斥的使用,并发限流
版权声明
本文为[椿尼]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_43080741/article/details/109203319
边栏推荐
- 202、快乐数
- 主流手机分辨率与尺寸
- Notes on concurrent programming of vegetables (IX) asynchronous IO to realize concurrent crawler acceleration
- MySQL how to merge the same data in the same table
- 全栈交叉编译X86完成过程经验分享
- 最强日期正则表达式
- 【leetcode】107. Sequence traversal of binary tree II
- Swagger2 自定义参数注解如何不显示
- Reading integrity monitoring techniques for vision navigation systems - 5 Results
- Jerry's more accurate determination of abnormal address [chapter]
猜你喜欢
Let the LAN group use the remote device
Notes on concurrent programming of vegetables (V) thread safety and lock solution
Six practices of Windows operating system security attack and defense
Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition
【leetcode】107.二叉树的层序遍历II
mysql同一个表中相同数据怎么合并
Jinglianwen technology - professional data annotation company and intelligent data annotation platform
得到知识服务app原型设计比较与实践
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
MapReduce core and foundation demo
随机推荐
Reading integrity monitoring techniques for vision navigation systems - 3 background
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
How does the swagger2 interface import postman
使用zerotier让异地设备组局域网
微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
主流手机分辨率与尺寸
MapReduce core and foundation demo
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
24、两两交换链表中的节点(链表)
Source insight 4.0 FAQs
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
部署jar包
242. Valid Letter ectopic words (hash table)
Solution architect's small bag - 5 types of architecture diagrams
349. Intersection of two arrays
Charles function introduction and use tutorial
Chapter 1 of technical Xiaobai (express yourself)
SQLServer 查询数据库死锁
Strongest date regular expression
209. Subarray with the smallest length (array)