Java 同步计数器CountDownLatch
天飞 2017-12-21 17:46:56 浏览37 评论0java 程序员 Json 同步 string static exception class void demo thread CountDownLatch
摘要: 再次记得进阶方向:适用场景。
再次记得进阶方向:适用场景。

package demo.thread;
import java.util.concurrent.CountDownLatch;
public class ThreadMain {
public static void main(String[] args) throws Exception {
CountDownLatch latch = new CountDownLatch(3);
Worker worker1 = new Worker("Jack 程序员1", latch);
Worker worker2 = new Worker("Rose 程序员2", latch);
Worker worker3 = new Worker("Json 程序员3", latch);
worker1.start();
worker2.start();
worker3.start();
latch.await();
System.out.println("Main thread end!");
}
static class Worker extends Thread {
private String workerName;
private CountDownLatch latch;
public Worker(String workerName, CountDownLatch latch) {
this.workerName = workerName;
this.latch = latch;
}
@Override
public void run() {
try {
System.out.println("Worker: " + workerName + " is begin.");
Thread.sleep(1000L);
System.out.println("Worker: " + workerName + " is end.");
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
}
}
Worker: Rose 程序员2 is begin.
Worker: Json 程序员3 is begin.
Worker: Jack 程序员1 is begin.
Worker: Jack 程序员1 is end.
Worker: Rose 程序员2 is end.
Worker: Json 程序员3 is end.
Main thread end! 本文为云栖社区原创内容,未经允许不得转载,如需转载请发送邮件至yqeditor@list.alibaba-inc.com;如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
用云栖社区APP,舒服~
【云栖快讯】中办国办印发《推进互联网协议第六版(IPv6)规模部署行动计划》加快推进基于 IPv6 的下一代互联网规模部署,计划指出2025年末中国 IPv6 规模要达到世界第一,阿里云也第一时间宣布了将全面提供IPv6服务,那么在全面部署 IPV6 前,你需要了解都在这儿 详情请点击 评论文章 (0) (0) (0)