当前位置:网站首页>关于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
边栏推荐
- Charles function introduction and use tutorial
- 【leetcode】102.二叉树的层序遍历
- Code implementation of general bubbling, selection, insertion, hill and quick sorting
- Read integrity monitoring techniques for vision navigation systems
- Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis
- Jerry's users how to handle events in the simplest way [chapter]
- HuggingFace
- The courses bought at a high price are open! PHPer data sharing
- UDP basic learning
- How to quickly download vscode
猜你喜欢

Reading integrity monitoring techniques for vision navigation systems - 5 Results

How to quickly download vscode

Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing

部署jar包

Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"

How does the swagger2 interface import postman

【leetcode】107.二叉树的层序遍历II

Notes on concurrent programming of vegetables (V) thread safety and lock solution

高价买来的课程,公开了!phper资料分享

Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
随机推荐
VIM usage
Source insight 4.0 FAQs
454、四数之和(哈希表)
解决方案架构师的小锦囊 - 架构图的 5 种类型
Detailed explanation of MapReduce calculation process
中职网络安全2022国赛之CVE-2019-0708漏洞利用
349、两个数组的交集
C#和数据库连接中类的问题
Esp32 learning - add folder to project
Learning Notes 6 - Summary of several deep learning convolutional neural networks
MySQL how to merge the same data in the same table
349. Intersection of two arrays
Latex usage
Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition
What are Jerry's usual program exceptions? [chapter]
Yarn core parameter configuration
Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
全栈交叉编译X86完成过程经验分享
Construction and traversal of binary tree
Resolution and size of mainstream mobile phones