CountDownLatch典型用法1:某一线程在开始运行前等待n个线程执行完毕。将CountDownLatch的计数器初始化为n new CountDownLatch(n) ,每当一个任务线程执行完毕,就将计数器减1 countdownlatch.countDown(),当计数器的值变为0时,在CountDownLatch上 await() 的线程就会被唤醒。一个典型应用场景就是启动一个服务时,主线程需要等待多个组件加载完毕,之后再继续执行。
-
/**
-
* CountDownLatch:多线程控制工具类,用来控制线程等待,它可以让某一个线程等待知道倒计时结束,再开始执行
-
*/
-
public
class CountDownLatchDemo {
-
-
//实例化并指定计数个数
-
private
static CountDownLatch countDown =
new CountDownLatch(
3);
-
-
public
static Runnable r1 =
new Runnable(){
-
@Override
-
public void run() {
-
try{
-
Thread.sleep(
1000);
-
System.out.println(
"r1 run");
-
countDown.countDown();
//个数-1
-
}
catch (Exception e){
-
e.printStackTrace();
-
}
-
-
}
-
};
-
-
public
static Runnable r2 =
new Runnable() {
-
@Override
-
public void run() {
-
try{
-
Thread.sleep(
2000);
-
System.out.println(
"r2 run");
-
countDown.countDown();
-
}
catch (Exception e){
-
e.printStackTrace();
-
}
-
}
-
};
-
-
public
static Runnable r3 =
new Runnable() {
-
@Override
-
public void run() {
-
try{
-
Thread.sleep(
1000);
-
System.out.println(
"r3 run");
-
countDown.countDown();
-
}
catch (Exception e){
-
e.printStackTrace();
-
}
-
}
-
};
-
-
public static void main(String[] args) throws Exception{
-
System.out.println(
"===begin=====");
-
ExecutorService exec = Executors.newFixedThreadPool(
3);
-
exec.submit(r1);
-
exec.submit(r2);
-
exec.submit(r3);
-
-
//等待检查,等待三个线程都执行完成后,主线程才能继续执行
-
countDown.await();
-
-
System.out.println(
"====end=====");
-
exec.shutdown();
-
}
-
}
输出结果:
===begin===== r1 run r3 run r2 run ====end=====
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count">1</span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/luzhensmart">
<img src="https://profile.csdnimg.cn/E/B/9/3_luzhensmart" class="avatar_pic" username="luzhensmart">
<img src="https://g.csdnimg.cn/static/user-reg-year/2x/8.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/luzhensmart" data-report-click="{"mod":"popu_379"}" target="_blank">这瓜保熟么</a></span>
</div>
<div class="text"><span>发布了150 篇原创文章</span> · <span>获赞 96</span> · <span>访问量 22万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=luzhensmart" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a>
</div>
</div>
</div>
CountDownLatch典型用法1:某一线程在开始运行前等待n个线程执行完毕。将CountDownLatch的计数器初始化为n new CountDownLatch(n) ,每当一个任务线程执行完毕,就将计数器减1 countdownlatch.countDown(),当计数器的值变为0时,在CountDownLatch上 await() 的线程就会被唤醒。一个典型应用场景就是启动一个服务时,主线程需要等待多个组件加载完毕,之后再继续执行。