当前位置:网站首页>关于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
边栏推荐
- 【leetcode】102.二叉树的层序遍历
- 图像处理——噪声小记
- 全栈交叉编译X86完成过程经验分享
- Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
- Construction and traversal of binary tree
- Comparison and practice of prototype design of knowledge service app
- SQL Server 递归查询上下级
- 454、四数之和(哈希表)
- Reading integrity monitoring techniques for vision navigation systems - 5 Results
- What are Jerry's usual program exceptions? [chapter]
猜你喜欢

How to quickly download vscode

Swagger2 自定义参数注解如何不显示

Manjaro installation and configuration (vscode, wechat, beautification, input method)

The courses bought at a high price are open! PHPer data sharing

Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis

Swagger2 接口如何导入Postman

Let the LAN group use the remote device

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

mysql同一个表中相同数据怎么合并

Jinglianwen technology - professional data annotation company and intelligent data annotation platform
随机推荐
Manjaro installation and configuration (vscode, wechat, beautification, input method)
206. Reverse linked list (linked list)
349. Intersection of two arrays
二叉树的构建和遍历
Swagger2 接口如何导入Postman
【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
域名和IP地址的联系
主流手机分辨率与尺寸
VIM usage
Leetcode22: bracket generation
SQL Server 递归查询上下级
景联文科技—专业数据标注公司和智能数据标注平台
App. In wechat applet JS files, components, APIs
MapReduce compression
24. Exchange the nodes in the linked list (linked list)
Qinglong panel pull library command update [April 20, 2022] collection is not lost
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
Derivation and regularization
Comparison and practice of prototype design of knowledge service app
19、删除链表的倒数第N个节点(链表)