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关键字保证了多线程的内存可 ...
随机推荐
- CHAKRA3 UART2
APP下: 配置BD文件: #define PADS_TCON_CONFIG Unknown_pad_mux #define PADS_UART2_MODE Unknown_pad_mux #defi ...
- UML统一建模语UML2和EnterpriseArchitect
其实前面的UML统一建模语言(一)所描述的都是UML1的内容,现在咱们聊一聊UML2. UML2.x完全建立在UML1.x基础之上,大多数的UML1.x模型在UML2.x中都可用.但UML2.x在结构 ...
- Python3解leetcode Maximum SubarrayClimbing Stairs
问题: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- 四 MySQL数据库表设计
一: 设计表: user: ID, PWD, name, type archiveRecord: referdate, archiveNum, owner, user, ...
- ICU 是一种说不出的痛啊
USE [Nursing] GO /****** Object: StoredProcedure [dbo].[P_GetICUVitualSign] Script Date: 05/21/2015 ...
- 双击jar不能运行的解决方法
1.问题描述 使用eclipse导出jar.双击jar文件弹出一个内容为“a java exception has occurred”的错误警告提示! 但是在命令行用 java -jar Em ...
- 《Java多线程编程核心技术》读后感(十四)
单例模式与多线程 立即加载/饿汉模式 立即加载就是使用类的时候已经将对象创建完毕,常见的实现办法就是直接new实例化. 立即加载/饿汉模式实在调用方法前,实例已经被创建了 package Six; p ...
- HDU - 6201 transaction transaction transaction(树形dp取两点)
transaction transaction transaction Kelukin is a businessman. Every day, he travels around cities to ...
- 安装coreseek 编译错误
Ubuntu下 编译csft 报错fatal error python.h no such file or directory 执行apt-get install python-dev即可
- 编写BeanFactory
/** * 一个创建Bean对象的工厂 * * Bean:在计算机英语中,有可重用组件的含义. * JavaBean:用java语言编写的可重用组件. * javabean > 实体类 * * ...