当前位置:网站首页>关于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
边栏推荐
- C language - custom type
- What are the system events of Jerry's [chapter]
- SQLServer 查询数据库死锁
- ID number verification system based on visual structure - Raspberry implementation
- Qinglong panel pull library command update [April 20, 2022] collection is not lost
- 全栈交叉编译X86完成过程经验分享
- Differences among restful, soap, RPC, SOA and microservices
- Resolution and size of mainstream mobile phones
- Download and installation steps of xshell + xftp
- /Can etc / shadow be cracked?
猜你喜欢

Example of pop-up task progress bar function based on pyqt5

使用zerotier让异地设备组局域网

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

Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.

SSH uses private key to connect to server without key

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

Reading integrity monitoring techniques for vision navigation systems - 3 background

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

Charles 功能介绍和使用教程

Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
随机推荐
Linked list intersection (linked list)
Read integrity monitoring techniques for vision navigation systems
Learning Notes 6 - Summary of several deep learning convolutional neural networks
707、设计链表(链表)
Full stack cross compilation x86 completion process experience sharing
解决方案架构师的小锦囊 - 架构图的 5 种类型
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
Read integrity monitoring techniques for vision navigation systems - 4 multiple faults in vision system
Xshell+Xftp 下载安装步骤
全栈交叉编译X86完成过程经验分享
SSH利用私钥无密钥连接服务器踩坑实录
高价买来的课程,公开了!phper资料分享
Strongest date regular expression
1、两数之和(哈希表)
Idea - indexing or scanning files to index every time you start
部署jar包
全栈交叉编译X86完成过程经验分享
VIM + ctags + cscope development environment construction guide
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
24、两两交换链表中的节点(链表)