Latch设计模式
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args){
// ProgrammerTravleTest.test();
ProgrammerTravleTest.test2();
}
}
/*
第23章 Latch 设计模式
若干线程并发执行某个特定的任务,然后等到所有的子任务都执行结束之后再统一汇总。
23.2 CountDownLatch程序实现
23.2.1 无限等待的Latch
*/
class WaitTimeoutException extends Exception{
public WaitTimeoutException(String msg) {
super(msg);
}
}
abstract class Latch{
protected int limit;
public Latch(int limit) {
this.limit = limit;
}
//该方法会使得当前线程一致等待
public abstract void await()throws InterruptedException;
//带超时功能的
public abstract void await(TimeUnit unit, long time) throws InterruptedException,WaitTimeoutException;
//当任务线程完成工作之后调用该方法使得计数器减一
public abstract void countDown();
//获取当前还有多少个线程没有完成任务
public abstract int getUnarrived();
}
//无限等待CountDownLatch实现
class CountDownLatch extends Latch{
public CountDownLatch(int limit) {
super(limit);
}
@Override
public void await() throws InterruptedException {
synchronized (this) {
while (limit > 0) {
this.wait();
}
}
}
/*
这个写法在写改写sysn时用过了。
*/
@Override
public void await(TimeUnit unit, long time) throws InterruptedException, WaitTimeoutException {
if (time < 0) {
throw new IllegalArgumentException("The time is invalid.");
}
long remainingNanos = unit.toNanos(time);
final long endNanos = System.nanoTime()+ remainingNanos;
synchronized (this) {
while (limit > 0) {
if (TimeUnit.NANOSECONDS.toMillis(remainingNanos) <= 0) {
throw new WaitTimeoutException("The wait time over specify time.");
}
this.wait(TimeUnit.NANOSECONDS.toMillis(remainingNanos));
remainingNanos=endNanos-System.nanoTime();
}
}
}
@Override
public void countDown() {
synchronized (this) {
if (limit <= 0) {
throw new IllegalArgumentException("all of task already arrived");
}
limit--;
this.notifyAll();
}
}
@Override
public int getUnarrived() {
return limit;
}
}
//程序测试齐心协力打开门阀
class ProgrammerTravle extends Thread{
private final Latch latch;
private final String programmer;
private final String transportation;
public ProgrammerTravle(Latch latch, String programmer, String transportation) {
this.latch = latch;
this.programmer = programmer;
this.transportation = transportation;
}
@Override
public void run() {
System.out.println(programmer+" start take the transportation["+transportation+"]");
try{
TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(programmer+" arrived by "+transportation);
latch.countDown();
}
}
class ProgrammerTravleTest{
public static void test(){
Latch latch = new CountDownLatch(4);
new ProgrammerTravle(latch,"A","a").start();
new ProgrammerTravle(latch,"B","b").start();
new ProgrammerTravle(latch,"C","c").start();
new ProgrammerTravle(latch,"D","d").start();
try {
latch.await();
System.out.println("==all of programmer arrived==");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void test2(){
Latch latch = new CountDownLatch(4);
new ProgrammerTravle(latch,"A","a").start();
new ProgrammerTravle(latch,"B","b").start();
new ProgrammerTravle(latch,"C","c").start();
new ProgrammerTravle(latch,"D","d").start();
try {
latch.await(TimeUnit.SECONDS,5);
System.out.println("==all of programmer arrived==");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (WaitTimeoutException e) {
e.printStackTrace();
}
}
}
《Java高并发编程详解》笔记
Latch设计模式的更多相关文章
- Android开发中常见的设计模式
对于开发人员来说,设计模式有时候就是一道坎,但是设计模式又非常有用,过了这道坎,它可以让你水平提高一个档次.而在android开发中,必要的了解一些设计模式又是非常有必要的.对于想系统的学习设计模式的 ...
- Spring源码分析 之浅谈设计模式
一直想专门写个Spring源码的博客,工作了,可以全身性的投入到互联网行业中.虽然加班很严重,但是依然很开心.趁着凌晨有时间,总结总结. 首先spring,相信大家都很熟悉了. 1.轻量级 零配置, ...
- 转 多线程 闭锁(Latch) 栅栏(CyclicBarrier)
java多线程并发系列之闭锁(Latch)和栅栏(CyclicBarrier) 标签: java并发编程 2015-05-28 16:45 2939人阅读 评论(0) 收藏 举报 本文章已收录于: . ...
- MVVM设计模式和WPF中的实现(四)事件绑定
MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- java EE设计模式简介
1.何为设计模式 设计模式提供了对常见应用设计问题的解决方案.在面向对象的编程中,设计模式通常在解决与对象创建和交互相关的问题,而非整体软件架构所面对的大规模问题,它们以样板代码的形式提供了通用的解决 ...
- 计算机程序的思维逻辑 (54) - 剖析Collections - 设计模式
上节我们提到,类Collections中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了第一类,本节我们介绍第二类. 第二类方法大概可以分为两组: 接受其他 ...
- 《JavaScript设计模式 张》整理
最近在研读另外一本关于设计模式的书<JavaScript设计模式>,这本书中描述了更多的设计模式. 一.创建型设计模式 包括简单工厂.工厂方法.抽象工厂.建造者.原型和单例模式. 1)简单 ...
- 《JavaScript设计模式与开发实践》整理
最近在研读一本书<JavaScript设计模式与开发实践>,进阶用的. 一.高阶函数 高阶函数是指至少满足下列条件之一的函数. 1. 函数可以作为参数被传递. 2. 函数可以作为返回值输出 ...
- 设计模式之行为类模式大PK
行为类模式大PK 行为类模式包括责任链模式.命令模式.解释器模式.迭代器模式.中介者模式.备忘录模式.观察者模式.状态模式.策略 ...
随机推荐
- JavaScript关于原型的相关内容
function Person () { } Person.prototype.name = 'Alan'; Person.prototype.age = 26; Person.prototype.j ...
- 微信小程序把玩(二十四)toast组件
原文:微信小程序把玩(二十四)toast组件 toast消息提示框,可用在提示一些信息,比如清楚缓存给用户一个友好的提示!或操作一些请求不想让用户有什么操作,toast也可以做到因为toast显示时其 ...
- 可视化文件消息收发一体化Socket实现V0.1
http://blog.csdn.net/laoyang360/article/details/8681918
- 如何解析DELPHI XE5服务器返回的JSON数据(翻译)及中文乱码
<span style="font-size:14px;">一直想找如何解析JSON数据的说,今天终于找到有人发帖子了.之前有人说用superobject,Tlkjso ...
- 插件化二(Android)
插件化二(Android) 上一篇文章<插件化一(android)>里大概构思了下插件加载与校验的流程和一些大体设计,这次就具体展开,在<动态加载与插件化>里提到以apk形式开 ...
- 每天进步一点--WCF学习笔记
最近买了一本书WCF服务编程,重头再开始了解学习WCF,现将学习记录,以便后来复习,也希望和大家一起进步. WCF用终结点表示一种组成关系,终结点就是地址.契约与绑定的混合品,即 地址(Address ...
- 对新数据库使用 Code First
如果使用的是 Visual Studio 2010,还需要安装 Nuget 1.创建应用程序 简单起见,我们将构建一个使用 Code First 执行数据访问的基本控制台应用程序. 打开 Visual ...
- C语言实现常用数据结构——队列
#include<stdio.h> #include<stdlib.h> #define MAX_SIZE 10 /* 用一个动态数组来实现队列 */ typedef stru ...
- abp(net core)+easyui+efcore仓储系统——定义仓储并实现 (四)
abp(net core)+easyui+efcore仓储系统目录 abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一) abp(net core)+easyui+e ...
- Linux下python多版本多环境介绍
一.python多版本配置说明 安装python相关依赖 [root@centos6 ~]# yum install -y gcc make patch gdbm-devel openssl-dev ...