CountDownLatch是java.util.concurrent并发包中提供的一个可用于控制多线程同时开始某动作的类。其采用的方式为减计数的方式。当计数减至零时,位于await后的代码才会执行。
CountDownLatch(int)
创建内部类Sync的对象实例,并将state设置为传入的参数。
await()
调用Sync继承的AbstractQueuedSynchronizer 的acquireShareInterruptibly完成。
acquireShareInterruptibly首先调用Sync的tryAcquireShared方法,该方法判断当前的state是否为零。如果为零,则返回1,否则返回-1。如果返回1,则await直接返回,如果返回-1,则将此线程放入队列进行等待,知道tryAcquireSharedfan返回-1或线程被interrupt
countdown()
调用Sync的tryReleaseShared方法,如果state不为零,基于CAS将state的值设置为减1后的值;如果减1后的值为零,则返回true,否则返回false
如果为true,则通知所有在队列等待的线程。
下面是一个例子
package DownTest;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDownLatchDemo {
private static final int PLAYER_AMOUNT = 5;
public CountDownLatchDemo() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 对于每位运动员,CountDownLatch减1后即结束比赛
CountDownLatch begin = new CountDownLatch(1);
// 对于整个比赛,所有运动员结束后才算结束
CountDownLatch end = new CountDownLatch(PLAYER_AMOUNT);
Player[] plays = new Player[PLAYER_AMOUNT];
for (int i = 0; i < PLAYER_AMOUNT; i++)
plays[i] = new Player(i + 1, begin, end);
// 设置特定的线程池,大小为5
ExecutorService exe = Executors.newFixedThreadPool(PLAYER_AMOUNT);
for (Player p : plays)
exe.execute(p); // 分配线程
System.out.println("Race begins!");
begin.countDown();
try {
end.await();
// 等待end状态变为0,即为比赛结束
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
System.out.println("Race ends!");
}
exe.shutdown();
}
}
对于Player类如下
package DownTest;
import java.util.concurrent.CountDownLatch;
public class Player implements Runnable {
private int id;
private CountDownLatch begin;
private CountDownLatch end;
public Player(int i, CountDownLatch begin, CountDownLatch end) {
// TODO Auto-generated constructor stub
super();
this.id = i;
this.begin = begin;
this.end = end;
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
begin.await(); //等待begin的状态为0
Thread.sleep((long)(Math.random()*100)); //随机分配时间,即运动员完成时间
System.out.println("Play"+id+" arrived.");
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}finally{
end.countDown(); //使end状态减1,最终减至0
}
}
}
执行结果:
Race begins!
Play2 arrived.
Play3 arrived.
Play5 arrived.
Play4 arrived.
Play1 arrived.
Race ends!
分享到:
相关推荐
CountDownLatch是什么? CountDownLatch是在java1.5被引入的,跟它一起被引入的并发工具类还有CyclicBarrier、Semaphore、ConcurrentHashMap和BlockingQueue,它们都存在于java.util.concurrent包下。CountDownLatch...
NULL 博文链接:https://cpjsjxy.iteye.com/blog/2272451
1.2 CountDownLatch概述 CountDownLatch的基本用法 2.1 创建CountDownLatch对象 2.2 await()方法 2.3 countDown()方法 实现多线程任务的同步 3.1 场景介绍 3.2 使用CountDownLatch实现同步 主线程等待多个线程完成 ...
主要为大家详细介绍了Java中CountDownLatch用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要介绍了JAVA多线程CountDownLatch的使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要介绍了如何使用CountDownLatch同步java多线程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
CountDownLatch、Semaphore等4大并发工具类详解,并介绍了简单的适用场景。
主要介绍了Java中CountDownLatch进行多线程同步详解及实例代码的相关资料,需要的朋友可以参考下
主要介绍了Java CountDownLatch完成异步回调实例详解的相关资料,需要的朋友可以参考下
主要为大家详细介绍了CountDownLatch的使用说明,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要介绍了Java线程并发工具类CountDownLatch原理及用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要为大家详细介绍了使用CountDownLatch等待多线程全部执行完成,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本篇文章主要介绍了SpringBoot CountDownLatch多任务并行处理的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了java利用CountDownLatch实现并行计算,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了Java并发系列之CountDownLatch源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本篇文章主要介绍了Java中多线程同步类 CountDownLatch的相关知识,具有很好的参考价值。下面跟着小编一起来看下吧
主要介绍了JAVA CountDownLatch(倒计时计数器)用法实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Java多线程编程之CountDownLatch同步工具使用实例,需要的朋友可以参考下
主要介绍了JAVA CountDownLatch与thread-join()的区别解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下