Listing -. Using a Countdown Latch to Trigger a Coordinated Start
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDownLatchDemo
{
final static int NTHREADS = ;
public static void main(String[] args)
{
final CountDownLatch startSignal = new CountDownLatch();
final CountDownLatch doneSignal = new CountDownLatch(NTHREADS);
Runnable r = new Runnable()
{
@Override
public void run()
{
try
{
report("entered run()");
startSignal.await(); // wait until told to ...
report("doing work"); // ... proceed
Thread.sleep((int) (Math.random() * ));
doneSignal.countDown(); // reduce count on which
// main thread is ...
} // waiting
catch (InterruptedException ie)
{
System.err.println(ie);
}
} void report(String s)
{
System.out.println(System.currentTimeMillis() +
": " + Thread.currentThread() + ": " + s);
}
}; ExecutorService executor = Executors.newFixedThreadPool(NTHREADS);
for (int i = ; i < NTHREADS; i++)
executor.execute(r);
try
{
System.out.println("main thread doing something");
Thread.sleep(); // sleep for 1 second
startSignal.countDown(); // let all threads proceed
System.out.println("main thread doing something else");
doneSignal.await(); // wait for all threads to finish
executor.shutdownNow();
}
catch (InterruptedException ie)
{
System.err.println(ie);
}
}
}

java CountDownLatch的更多相关文章

  1. Java --CountDownLatch简介

    CountDownLatch 1.类介绍 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.用给定的计数 初始化 CountDownLatch.由于调用了 coun ...

  2. java CountDownLatch 使用介绍

    CountDownLatch是在java1.5被引入的,跟它一起被引入的并发工具类还有CyclicBarrier.Semaphore.ConcurrentHashMap和BlockingQueue,它 ...

  3. 跟着ZooKeeper学Java——CountDownLatch和Join的使用

    在阅读ZooKeeper的源码时,看到这么一个片段,在单机模式启动的时候,会调用下面的方法,根据zoo.cfg的配置启动单机版本的服务器: public void runFromConfig(Serv ...

  4. java CountDownLatch的使用

    CountDownLatch能够使一个线程在等待另外一些线程完成各自工作之后,再继续执行.使用一个计数器进行实现.计数器初始值为线程的数量.当每一个线程完成自己任务后,计数器的值就会减一.当计数器的值 ...

  5. Java CountDownLatch解析(上)

    写在前面的话 最近一直在边工作边学习分布式的东西,看到了构建Java中间件的基础知识,里面有提到Java多线程并发的工具类,例如ReentrantLock.CyclicBarrier.CountDow ...

  6. Java CountDownLatch解析(下)

    写在前面的话 在上一篇CountDownLatch解析中,我们了解了CountDownLatch的简介.CountDownLatch实用场景.CountDownLatch实现原理中的await()方法 ...

  7. java CountDownLatch 控制异步和同步

    应用场景举例: 执行A项目的方法,需要调用B项目.C项目.D项目的接口方法. 需求: 异步调用B.C.D项目的接口方法,且每个接口都调用结束后,A项目的方法才可以结束. 注:如果需要获取接口返回结果, ...

  8. java CountDownLatch 等待多线程完成

    CountDownLatch允许一个或多个线程等待其他线程完成操作. package com.test; import java.util.concurrent.CountDownLatch; pub ...

  9. java CountDownLatch报错java.lang.IllegalMonitorStateException: null

    笔者使用websocket进行通信,服务器异步返回.websocket服务器又异步调用其他websocket,也是异步访问. 由于无法预测服务器调用第三方websocket什么时候调用结束,使用了Co ...

随机推荐

  1. (int &)a 和(int)a

      [cpp] view plain copy float a = 1.0f;     cout < < (int)a < < endl;     cout < < ...

  2. D. Array GCD

    You are given array ai of length n. You may consecutively apply two operations to this array: remove ...

  3. Json格式数据某一列。

    {title : '支付费率',elCls : 'center', dataIndex :'zfrate',width:100, renderer :function(value,obj){ if(o ...

  4. 操作properties文件,注意抹掉最前面的"file:"

    package com.xiewanzhi.property; import java.io.BufferedInputStream; import java.io.File; import java ...

  5. JAVA 获取jdbc.properties配置信息

    Properties myProperty = new Properties();String jdbcPath = PathKit.getWebRootPath()+File.separator+& ...

  6. c++ namespace的使用

    ** namespace:命名空间就是为解决C++中的变量.函数的命名冲突而服务的. ** namespace定义的格式基本格式是: namespace identifier {    entitie ...

  7. (转载)c# winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值

    第一种方法: 用委托,Form2和Form3是同一组 Form2 C#代码 using System; using System.Collections.Generic; using System.C ...

  8. iis浏览网页时提示无法显示 XML 页

    无法显示 XML 页.         使用 样式表无法查看 XML 输入.请更正错误然后单击 刷新按钮,或以后重试.          处理资源 'http://localhost/ 时出错.第 1 ...

  9. dos界面下执行java文件将错误输出到一个文本小技巧

    如果dos下执行java出现错误,把错误记录到一个文档 正确时如图,输出结果为hello,我把String的s改为小写,出现错误,用2>命令输出到error.txt在当前目录就出现了error. ...

  10. [转].net中的认证(authentication)与授权(authorization)

    本文转自:http://www.cnblogs.com/yjmyzz/archive/2010/08/29/1812038.html 注:这篇文章主要给新手看的,老手们可能会觉得没啥营养,就请绕过吧. ...