Java并发工具类CountDownLatch源码中的例子
Java并发工具类CountDownLatch源码中的例子
实例一
原文描述
/**
* <p><b>Sample usage:</b> Here is a pair of classes in which a group
* of worker threads use two countdown latches:
* <ul>
* <li>The first is a start signal that prevents any worker from proceeding
* until the driver is ready for them to proceed;
* <li>The second is a completion signal that allows the driver to wait
* until all workers have completed.
* </ul>
**/
样本用法:这是一对组中的一个类工作线程使用两个倒计时锁存器:
第一个是启动信号,阻止任何工作人员继续进行直到司机准备好继续进行;
第二个是完成信号,允许驾驶员等待直到所有工人完成。
实例代码
public class Driver {
public static void main(String[] args) throws InterruptedException {
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(5);
for (int i=0;i<5;++i){
new Thread(new Worker(startSignal, doneSignal)).start();
}
System.out.println("ALL THREAD START UP.");
// 任务开始信号
startSignal.countDown();
System.out.println(" startSignal count down. ");
// 等待所有线程执行完成后才执行后续代码
doneSignal.await();
System.out.println(" ALL THREAD END UP. ");
// 执行结果
// ALL THREAD START UP.
// startSignal count down.
// Hello World!
// Hello World!
// Hello World!
// Hello World!
// Hello World!
// ALL THREAD END UP.
}
}
class Worker implements Runnable{
private final CountDownLatch startSignal;
private final CountDownLatch doneSignal;
Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
this.startSignal = startSignal;
this.doneSignal = doneSignal;
}
@Override
public void run() {
try {
// 阻止线程执行,直到startSignal.countDown()后开始执行
startSignal.await();
doWork();
doneSignal.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
void doWork(){
System.out.println("Hello World!");
}
}
实例二
原文描述
/*
* <p>Another typical usage would be to divide a problem into N parts,
* describe each part with a Runnable that executes that portion and
* counts down on the latch, and queue all the Runnables to an
* Executor. When all sub-parts are complete, the coordinating thread
* will be able to pass through await. (When threads must repeatedly
* count down in this way, instead use a {@link CyclicBarrier}.)
**/
另一种典型用法是将问题分成N个部分,用执行该部分的Runnable描述每个部分倒计时锁定,并将所有Runnables排队到执行人。
当所有子部件完成时,协调线程将能够通过等待。(当线程必须重复时以这种方式倒数,而不是使用{@link CyclicBarrier})
实例代码
public class Driver2 {
public static void main(String[] args) throws InterruptedException {
CountDownLatch doneSignal = new CountDownLatch(5);
// 创建线程池
Executor executor = Executors.newSingleThreadExecutor();
for (int i = 0;i<5;++i){
// create and start threads
executor.execute(new WorkerRunnable(doneSignal,i));
}
// 等待所有线程任务执行完成
doneSignal.await();
System.out.println("EXECUTOR DOWN.");
// 关闭线程池
((ExecutorService) executor).shutdown();
// 输出结果
// Hello World! : 0
// Hello World! : 1
// Hello World! : 2
// Hello World! : 3
// Hello World! : 4
// EXECUTOR DOWN.
}
}
class WorkerRunnable implements Runnable {
private final CountDownLatch doneSignal;
private final int i;
WorkerRunnable(CountDownLatch doneSignal, int i) {
this.doneSignal = doneSignal;
this.i = i;
}
@Override
public void run() {
doWork(i);
doneSignal.countDown();
}
void doWork(int count) {
System.out.println("Hello World! : " + count);
}
}
Java并发工具类CountDownLatch源码中的例子的更多相关文章
- Java并发工具类 - CountDownLatch
Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...
- 25.大白话说java并发工具类-CountDownLatch,CyclicBarrier,Semaphore,Exchanger
1. 倒计时器CountDownLatch 在多线程协作完成业务功能时,有时候需要等待其他多个线程完成任务之后,主线程才能继续往下执行业务功能,在这种的业务场景下,通常可以使用Thread类的join ...
- java 并发工具类CountDownLatch & CyclicBarrier
一起在java1.5被引入的并发工具类还有CountDownLatch.CyclicBarrier.Semaphore.ConcurrentHashMap和BlockingQueue,它们都存在于ja ...
- JAVA并发工具类---------------(CountDownLatch和CyclicBarrier)
CountDownLatch是什么 CountDownLatch,英文翻译为倒计时锁存器,是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 闭锁可以延迟线程的进 ...
- Java 并发工具类 CountDownLatch、CyclicBarrier、Semaphore、Exchanger
本文部分摘自<Java 并发编程的艺术> CountDownLatch CountDownLatch 允许一个或多个线程等待其他线程完成操作.假设现有一个需求:我们需要解析一个 Excel ...
- Java并发系列[7]----CountDownLatch源码分析
CountDownLatch(闭锁)是一个很有用的工具类,利用它我们可以拦截一个或多个线程使其在某个条件成熟后再执行.它的内部提供了一个计数器,在构造闭锁时必须指定计数器的初始值,且计数器的初始值必须 ...
- Java中的4个并发工具类 CountDownLatch CyclicBarrier Semaphore Exchanger
在 java.util.concurrent 包中提供了 4 个有用的并发工具类 CountDownLatch 允许一个或多个线程等待其他线程完成操作,课题点 Thread 类的 join() 方法 ...
- Java并发系列[2]----AbstractQueuedSynchronizer源码分析之独占模式
在上一篇<Java并发系列[1]----AbstractQueuedSynchronizer源码分析之概要分析>中我们介绍了AbstractQueuedSynchronizer基本的一些概 ...
- Java并发系列[5]----ReentrantLock源码分析
在Java5.0之前,协调对共享对象的访问可以使用的机制只有synchronized和volatile.我们知道synchronized关键字实现了内置锁,而volatile关键字保证了多线程的内存可 ...
随机推荐
- 【Lintcode】029.Interleaving String
题目: Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2 ...
- 【Lintcode】036.Reverse Linked List II
题目: Reverse a linked list from position m to n. Given m, n satisfy the following condition: 1 ≤ m ≤ ...
- 微软开业网站----精华 http://www.microsoft.com/opensource/directory.aspx
http://www.microsoft.com/opensource/directory.aspx
- YOLO3训练widerface数据集
因为YOLO3速度精度都很棒,所以想训练一下人脸模型,废话不多,进入正题 1写所有的配置文件 1.1 YOLO3-face.cfg 个人感觉YOLO的配置文件骑士和caffe差不多 在cfg/YOLO ...
- C# FileStream分块读取和保存文件
一 FileStream分块读取文件 public byte[] GetFileData(string fileName, long startPosition, long length) { byt ...
- error: field has incomplete type
在头文件使用某一自定义的类的指针或引用时,只需要前置声明该类即可,然而如果该类中有静态成员时,必须包含该类的头文件,而不是使用前置声明.
- IE各栏的截图说明
工具栏 包括 状态栏 命令栏 菜单栏 收藏栏 IE工具 > 工具栏 > 状态栏 有状态栏显示 无状态栏显示 菜单栏 快捷键 alt 可以快速展示 菜单栏 ,查看 ...
- QDUOJ ycb的ACM进阶之路 二进制多重背包
ycb的ACM进阶之路 发布时间: 2017年5月22日 14:30 最后更新: 2017年5月22日 14:31 时间限制: 1000ms 内存限制: 128M 描述 ycb是个天资聪颖 ...
- 51nod1100(计算斜率)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1100 题意:中文题啦- 思路:算斜率不用多说吧?本题唯一一个 ...
- uva11584 Partitioning by Palindromes
题目大意: 给出一个字符串,把他划分成尽量少的回文串,问最少的回文串个数 /* 先预处理所有回文子串 dp[i]表示字符1~i划分成的最小回文串的个数 */ #include<iostream& ...