Latch设计模式
2024-08-27 13:36:45
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高并发编程详解》笔记
最新文章
- 崽崽帮www.zaizaibang.com精选14
- AOP是什么?
- Linux 集群
- 高性能MySQL笔记 第6章 查询性能优化
- javascript中IE与ff的区别
- 中间人攻击之arp欺骗 科普ARP欺骗
- C#自定义线程池
- 我和CPP的第二次约会
- 整理HTML的一些基础
- Android使用ViewFlipper实现左右滑动效果面
- Linux下打造全方位立体监控系统
- 华为6.0系统设备最完美激活Xposed框架的经验
- namenode做了ha后kylin出现错误No registered coprocessor service found for name CubeVisitService in region
- ctrl c 中文字符到 vnc 里,中文字符已经被转码
- Qt532,鼠标键盘事件_ZC
- JavaScript:几种常用循环
- Spring---七大核心模块
- python3 安装
- :Linux 系统日志管理 日志转储
- JavaScript练习笔记整理&#183;1 - 6.23
热门文章
- 简单的Windows Webcam应用:Barcode Reader
- delphi多版本安装方法
- 将QT开发的界面程序封装成DLL,在VC中成功调用(必须有消息循环,所以使用了QTWinmigrate,附CSDN可下载的Demo)
- redis的下载及使用
- java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory 解决办法
- linux Apache设置https访问以及加载mod_ssl.so模块以及问题解决
- spring_three
- 在C#中用静态类来扩展类的方法
- FPM
- oracle 向数据库同时插入多条数据