JUC提供的几种线程之间协作的工具类
2020-12-17 01:08
标签:ase 循环 bar 一个 ble 运动员 over get one
* CyclicBarrier的对象是一个一个的线程
* CountDownLatch则是一个又一个的事件
*/
@Test
public void testCyclicBarrier() throws InterruptedException {
int num = 3;
CyclicBarrier cyclicBarrier = new CyclicBarrier(num);
LOGGER.info("等待第一次讨论");
for (int i = 0; i JUC提供的几种线程之间协作的工具类 标签:ase 循环 bar 一个 ble 运动员 over get one 原文地址:https://www.cnblogs.com/plumswine/p/14118324.html
CountDownLatch 倒计时门闩
/**
* CountDownLatch不能被重用, 如果需要重新计数, 可以考虑CyclicBarrier
* all wait for onw, 如多个运动员等待裁判员鸣枪, 比赛开始
* one wait for all, 如等待所有运动员到达终点, 比赛结束
*/
@Test
public void testCountDownLatch() throws InterruptedException {
int size = 10;
final CountDownLatch end = new CountDownLatch(size);
final CountDownLatch begin = new CountDownLatch(1);
for (int i = 0; i
CyclicBarrier 循环珊栏
/**
* 等待固定线程到达了珊栏位置, 所有线程才能继续向下执行
* 类似于在等待所有人都到齐后, 大家才开始讨论工作
*
Semaphore 信号量
@Test
public void testSemaphore() throws InterruptedException {
// true参数表示是否公平, 默认是非公平
final Semaphore semaphore = new Semaphore(3, true);
for (int i = 0; i
Condition 条件对象
/**
* Condition的signal方法必须在持有锁的情况下才能执行
*/
@Test
public void testCondition() throws InterruptedException {
ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();
for (int i = 0; i
文章标题:JUC提供的几种线程之间协作的工具类
文章链接:http://soscw.com/index.php/essay/36811.html