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. 【原创】请避免GO语言中的携程空跑(CPU突然激增)

    其实GO语言从1.6版本开始非常不错了,GC性能优化非常到位,并且各种并行设计比从新实现一套C++版本的确是方便不少. 语言包也很多,库也相对稳定,完全可以适用于生产环境. 本文主要是给刚刚入门新手注 ...

  2. Python中ndarray数组切片问题a[-n -x:-y]

    先看看如下代码: >>a=np.arange(10)>>a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>a[-7:] array( ...

  3. C程序设计语言(2)文摘

    第一章 导言 1.1 入门 1.2 变量与算术表达式 1.3 for语句 1.4 符号常量 1.5 字符输入输出 #include "stdafx.h" main(int argc ...

  4. UVaLive 3126 Taxi Cab Scheme (最小路径覆盖)

    题意:有 n 个客人,要从 si 到 ti,每个人有一个出发时间,现在让你安排最少和出租车去接,在接客人时至少要提前一分钟到达客人的出发地点. 析:把每个客人看成一个结点,然后如果用同一个出租车接的话 ...

  5. iOS7修改UISearchBar的Cancel按钮的颜色和文字

    两行代码搞定: [[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor ...

  6. (最短路 SPFA)Currency Exchange -- poj -- 1860

    链接: http://poj.org/problem?id=1860 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2326 ...

  7. 桥接模式及C++实现

    桥接模式 先说说桥接模式的定义:将抽象化(Abstraction)与实现化(Implementation)分离,使得二者可以独立地变化. 桥接模式号称设计模式中最难理解的模式之一,关键就是这个抽象和实 ...

  8. HDU1233 还是畅通工程 2017-04-12 19:49 64人阅读 评论(0) 收藏

    还是畅通工程 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  9. Java ActiveMQ 讲解(一)理解JMS 和 ActiveMQ基本使用

    最近的项目中用到了mq,之前自己一直在码农一样的照葫芦画瓢.最近几天研究了下,把自己所有看下来的文档和了解总结一下. 一. 认识JMS 1.概述 对于JMS,百度百科,是这样介绍的:JMS即Java消 ...

  10. pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

    # 背景 安装pip后发现执行pip install pytest,提示下面错误 pip is configured with locations that require TLS/SSL, howe ...