CountDownLatch的简单实现
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的简单实现的更多相关文章
- CountDownLatch的简单使用
from https://www.jianshu.com/p/cef6243cdfd9 1.CountDownLatch是什么? CountDownLatch是一个同步工具类,它允许一个或多个线程一直 ...
- CountDownLatch的简单讲解
正如每个Java文档所描述的那样,CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行.在Java并发中,countdownlatch的概念是一 ...
- CountDownLatch的简单理解
CountDownLatch的概念 CountDownLatch是一个同步工具类,用来协调多个线程之间的同步,或者说起到线程之间的通信(而不是用作互斥的作用). CountDownLatch能够使一个 ...
- JAVA中CountDownLatch的简单示例
public static void main(String[] args) throws InterruptedException { CountDownLatch latch =new Count ...
- CountDownLatch的原理学习
转载:http://blog.csdn.net/yanyan19880509/article/details/52349056 前言 前面介绍了ReentrantLock,又叫排他锁,本篇主要通过Co ...
- CountDownLatch 和 CyclicBarrier 的运用及实现原理
I.CountDownLatch 和 CyclicBarrier 的运用 CountDownlatch: 定义: 其是一个线程同步的辅助工具,通过它可以做到使一条线程一直阻塞等待,直到其他线程完成其所 ...
- java共享锁实现原理及CountDownLatch解析
前言 前面介绍了ReentrantLock,又叫排他锁,本篇主要通过CountDownLatch的学习来了解java并发包中是如何实现共享锁的. CountDownLatch使用解说 CountDow ...
- java高级---->Thread之CountDownLatch的使用
CountDownLatch是JDK 5+里面闭锁的一个实现,允许一个或者多个线程等待某个事件的发生.今天我们通过一些实例来学习一下它的用法. CountDownLatch的简单使用 CountDow ...
- Java并发编程之CountDownLatch,CyclicBarrier实现一组线程相互等待、唤醒
java多线程应用场景不少,有时自己编写代码又不太容易实现,好在concurrent包提供了不少实现类,还有google的guava包更是提供了一些最佳实践,这让我们在面对一些多线程的场景时,有了不少 ...
随机推荐
- webform调用windows服务
准备工作: .电脑->管理->本地用户和组->组->Administrator双击->隶属->添加Network service->确定 .启动windows ...
- jdbc和Java中的日期问题
JDBC中的日期Java.sql.Date 是继承自Java中的Java.util.Date,在实现插入的时候可以 将Java.util.Date类型的时间转换成毫秒数,date.getTime(), ...
- Gym 101201J Shopping (线段树+取模)
题意:给定 n 个物品,然后有 m 个人买东西,他们有 x 元钱,然后从 l - r 这个区间内买东西,对于每个物品都尽可能多的买,问你最少剩下多少钱. 析:对于物品,尽可能多的买的意思就是对这个物品 ...
- HDU 1286 找新朋友 (欧拉phi函数打表)
题意:你懂得. 析:一看这个题应该是欧拉phi函数,也就说欧拉phi函数是指求从 1 到 n 中与 n 互素的数的个数,这个题很明显是这个意思嘛,不多说了. 代码如下: #include <io ...
- 看图说说JVM老年代垃圾收集器
注:G1垃圾收集器是目前最前沿的GC收集器,未来将取代CMS垃圾收集器,可以作为整个Heap的收集器使用,不限于老年代!!!
- Shell编程-01-Shell脚本初步入门
目录 什么是Shell 什么是Shell脚本 Shell脚本语言的种类 常用操作系统默认Shell Shell 脚本的建立和执行 脚本规范 什么是Shell 简单来说Shell其实就是一个命令 ...
- Windbg and resources leaks in .NET applications 资源汇总
Windows Forms Leaks 1.http://blogs.msdn.com/b/tess/archive/2008/02/04/net-debugging-demos-informatio ...
- Postgresql 分区表 一
Postgres 10 新特性 分区表 http://francs3.blog.163.com/blog/static/40576727201742103158135/ Postgres 10 之前分 ...
- 基于pscp批量分发文件
用法: pscp -h 目标ip文件 本地文件路径 远程路径 pscp -h hosts.ip file.txt ~/
- c# 检查报错详细
catch (DbEntityValidationException error) { string test = string.Empty; foreach (var validationError ...