Java实例学习 Java并发编程之java.util.concurrent.CountDownLatch
来源:互联网 发布:影子网络杀人图 编辑:程序博客网 时间:2024/05/16 05:03
CountDownLatch是一种简单的同步模式,
它让一个线程可以等待一个或多个线程完成它们的工作从而避免对临界资源并发访问所引发的各种问题。
import java.util.concurrent.CountDownLatch;
/**
* 工人类
*/
class Worker {
private String name; // 名字
private long workDuration; // 工作持续时间
/**
* 构造器
*/
public Worker(String name, long workDuration) {
this.name = name;
this.workDuration = workDuration;
}
/**
* 完成工作
*/
public void doWork() {
System.out.println(name + " begins to work...");
try {
Thread.sleep(workDuration); // 用休眠模拟工作执行的时间
} catch(InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(name + " has finished the job...");
}
}
/**
* 测试线程
*/
class WorkerTestThread implements Runnable {
private Worker worker;
private CountDownLatch cdLatch;
public WorkerTestThread(Worker worker, CountDownLatch cdLatch) {
this.worker = worker;
this.cdLatch = cdLatch;
}
@Override
public void run() {
worker.doWork(); // 让工人开始工作
cdLatch.countDown(); // 工作完成后倒计时次数减1
}
}
public class CountDownLatchTest {
private static final int MAX_WORK_DURATION = 5000; // 最大工作时间
private static final int MIN_WORK_DURATION = 1000; // 最小工作时间
// 产生随机的工作时间
private static long getRandomWorkDuration(long min, long max) {
return (long) (Math.random() * (max - min) + min);
}
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(2); // 创建倒计时闩并指定倒计时次数为2
Worker w1 = new Worker("骆昊", getRandomWorkDuration(MIN_WORK_DURATION, MAX_WORK_DURATION));
Worker w2 = new Worker("王大锤", getRandomWorkDuration(MIN_WORK_DURATION, MAX_WORK_DURATION));
new Thread(new WorkerTestThread(w1, latch)).start();
new Thread(new WorkerTestThread(w2, latch)).start();
try {
latch.await(); // 等待倒计时闩减到0
System.out.println("All jobs have been finished!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
转载自:http://blog.csdn.net/jackfrued/article/details/44499227
- Java实例学习 Java并发编程之java.util.concurrent.CountDownLatch
- java.util.concurrent之CountDownLatch
- Java实例学习 Java并发编程之java.util.concurrent.CopyOnWriteArrayList
- java.util.concurrent.CountDownLatch
- java.util.concurrent.CountDownLatch
- java.util.concurrent.CountDownLatch
- java.util.concurrent.CountDownLatch用法
- 初识java.util.concurrent.CountDownLatch
- java.util.concurrent|CountDownLatch使用
- Java并发编程之java.util.concurrent包UML结构
- java并发编程:java.util.concurrent
- java并发编程(3)--java.util.concurrent.*
- Java并发包concurrent之CountDownLatch
- java-concurrent之CountDownLatch
- java-concurrent之CountDownLatch
- java.util.concurrent.CyclicBarrier 与 java.util.concurrent.CountDownLatch
- Java多线程工具包java.util.concurrent---CountDownLatch
- Java 并发编程之 CountDownLatch
- DMG文件介绍及建立
- android 百度地图获取左上角和右下角的坐标
- gephi打开提示can't find java1.6 or higer
- html和css学习小结
- Leetcode 46-47. Permutations backtrack
- Java实例学习 Java并发编程之java.util.concurrent.CountDownLatch
- mac上可以使用的Windows远程桌面APP
- 闭包
- 最新全国身份证号码对照sql脚本
- 234. Palindrome Linked List
- as3 复制内容到剪贴板
- JAVA 数组常用方法
- 【Android-View】浅谈Android换肤的几种实现方式
- traitlets.traitlets.TraitError in Pycharm