1.

@Data
public abstract class BaseLatch {
private int limit;
protected int running; BaseLatch(int limit) {
this.limit = limit;
running = limit;
} public abstract void await() throws InterruptedException; public abstract void await(long ms) throws InterruptedException, TimeoutException; public abstract void countDown();
} class CountDownLatch extends BaseLatch { public CountDownLatch(int limit) {
super(limit);
} @Override
public void await() throws InterruptedException {
synchronized (this) {
while (this.running > 0) {
this.wait();
}
}
} @Override
public void await(long ms) throws InterruptedException, TimeoutException {
Assert.isTrue(ms > 0, "不允许小于0");
final long endTime = System.currentTimeMillis() + ms;
synchronized (this) {
while (this.running > 0) {
long balanceTime = endTime - System.currentTimeMillis();
if (balanceTime <= 0) {
throw new TimeoutException();
}
this.wait(balanceTime);
}
}
} @Override
public void countDown() {
synchronized (this) {
if (this.running <= 0) {
throw new IllegalStateException("");
}
this.running--;
this.notifyAll();
}
}
} class LatchTest {
public static void main(String[] args) throws InterruptedException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Work<Integer>[] works = new Work[2];
BaseLatch latch = new CountDownLatch(works.length);
for (int i = 0; i < works.length; i++) {
works[i] = new Work<Integer>(latch);
new Thread(works[i]).start();
}
try {
latch.await(5000);
} catch (TimeoutException ex) { }
for (int i = 0; i < works.length; i++) {
System.out.println(works[i].getResult());
}
stopWatch.stop();
System.out.println("共消耗时间:" + stopWatch.getTotalTimeSeconds() + "秒");
}
} class Work<T> extends Thread {
private BaseLatch latch;
private T result; public Work(BaseLatch latch) {
this.latch = latch;
} @Override
public void run() {
try {
Random random = new Random();
int ms = random.nextInt(10) + 1;
Thread.sleep(1000 * ms);
this.result = (T) (Object) ms;
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
latch.countDown();
}
} public T getResult() {
return result;
}
}

2.可能的输出如下

null
2
共消耗时间:5.012秒

CountDownLatch的简单实现的更多相关文章

  1. CountDownLatch的简单使用

    from https://www.jianshu.com/p/cef6243cdfd9 1.CountDownLatch是什么? CountDownLatch是一个同步工具类,它允许一个或多个线程一直 ...

  2. CountDownLatch的简单讲解

    正如每个Java文档所描述的那样,CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行.在Java并发中,countdownlatch的概念是一 ...

  3. CountDownLatch的简单理解

    CountDownLatch的概念 CountDownLatch是一个同步工具类,用来协调多个线程之间的同步,或者说起到线程之间的通信(而不是用作互斥的作用). CountDownLatch能够使一个 ...

  4. JAVA中CountDownLatch的简单示例

    public static void main(String[] args) throws InterruptedException { CountDownLatch latch =new Count ...

  5. CountDownLatch的原理学习

    转载:http://blog.csdn.net/yanyan19880509/article/details/52349056 前言 前面介绍了ReentrantLock,又叫排他锁,本篇主要通过Co ...

  6. CountDownLatch 和 CyclicBarrier 的运用及实现原理

    I.CountDownLatch 和 CyclicBarrier 的运用 CountDownlatch: 定义: 其是一个线程同步的辅助工具,通过它可以做到使一条线程一直阻塞等待,直到其他线程完成其所 ...

  7. java共享锁实现原理及CountDownLatch解析

    前言 前面介绍了ReentrantLock,又叫排他锁,本篇主要通过CountDownLatch的学习来了解java并发包中是如何实现共享锁的. CountDownLatch使用解说 CountDow ...

  8. java高级---->Thread之CountDownLatch的使用

    CountDownLatch是JDK 5+里面闭锁的一个实现,允许一个或者多个线程等待某个事件的发生.今天我们通过一些实例来学习一下它的用法. CountDownLatch的简单使用 CountDow ...

  9. Java并发编程之CountDownLatch,CyclicBarrier实现一组线程相互等待、唤醒

    java多线程应用场景不少,有时自己编写代码又不太容易实现,好在concurrent包提供了不少实现类,还有google的guava包更是提供了一些最佳实践,这让我们在面对一些多线程的场景时,有了不少 ...

随机推荐

  1. Please ensure that adb is correctly located at 。。。。。。。。。。。。

    遇到问题描述: 运行Android程序控制台输出 [2012-07-18 16:18:26 - ] The connection to adb is down, and a severe error ...

  2. Undo Architecture

    [Undo Architecture] NSUndoManager is a general-purpose recorder of operations for undo and redo. NSU ...

  3. Spring Boot配置FastJson报错'Content-Type' cannot contain wildcard type '*'

    升级到最新版本的fastjson以后报的错,查了一下资料,发现 fastjson从1.1.41升级到1.2.28之后,请求报错:json java.lang.IllegalArgumentExcept ...

  4. System.Web.HttpUtility VS System.Web.HttpServerUtility VS System.Net.WebUtility

    HttpUtility 类作为 HttpServerUtility 类的内部使用,HttpServerUtility 通过System.Web.UI.Page.Server属性(WebForm)/Co ...

  5. jmeter 各种配置修修改(后续增加)

    1.修改物理内存  使用jmeter进行压力测试时遇到一段时间后报内存溢出outfmenmory错误,导致jmeter卡死了,先尝试在jmeter.bat中增加了JVM_ARGS="-Xmx ...

  6. 23 DesignPatterns学习笔记:C++语言实现 --- 2.7 Proxy

    23 DesignPatterns学习笔记:C++语言实现 --- 2.7 Proxy 2016-07-18 (www.cnblogs.com/icmzn) 模式理解

  7. POJ 3977 Subset(折半枚举+二分)

    SubsetTime Limit: 30000MS        Memory Limit: 65536KTotal Submissions: 6754        Accepted: 1277 D ...

  8. persona 典型用户

    1.姓名:王涛 2.年龄:22 3.收入:基本无收入 4.代表用户在市场上的比例和重要性:王涛为铁道学生.本软件的用户主要是学生和老师,尤其是广大的铁大学子,所以此典型用户的重要性不言而喻,而且比例相 ...

  9. Oracle FND API–Create User

    --API - fnd_user_pkg.createuser----Example -- -- ---------------------------------------- API to CRE ...

  10. ORACLE PATCH 版本的查询 PL/SQL

    --ORACLE PATCH 版本的查询 PL/SQL SELECT DD.PATCH_NAME,        PP.CREATION_DATE,        PP.DRIVER_FILE_NAM ...