当前位置: 首页 > news >正文

【2013-12-16】《Java编程思想》读书笔记:CountDownLatch

[历史归档]本文原发布于 cstriker1407.info 个人博客,内容为历史存档,仅供参考。
发布时间:2013-12-16| 标题:《Java编程思想》读书笔记:CountDownLatch分类:编程 / java / think_in_java |标签:java·think in java·多线程·CountDownLatch


《Java编程思想》读书笔记:CountDownLatch

最近在整理 Java 的多线程相关的技术,这里备份下关于【 CountDownLatch 】代码。

github:

【 https://github.com/cstriker1407/think_in_java 】

CountDownLatch主要用来完成一个计数器锁的业务,即计数完成前线程会等待。这里备份下代码:

publicclassCountDownLatchTest{publicstaticvoidtest(){CountDownLatchstartSignal=newCountDownLatch(1);CountDownLatchdoneSignal=newCountDownLatch(3);for(inti=0;i<3;++i){newThread(newWorker(startSignal,doneSignal)).start();}try{Thread.sleep(1000);}catch(InterruptedExceptione1){e1.printStackTrace();}System.out.println("the runnables are inited");System.out.println("the runnables are free to start");startSignal.countDown();try{doneSignal.await();}catch(InterruptedExceptione){e.printStackTrace();}}}classWorkerimplementsRunnable{privatestaticintcount=0;privateintidx=0;privatefinalCountDownLatchstartSignal;privatefinalCountDownLatchdoneSignal;Worker(CountDownLatchstartSignal,CountDownLatchdoneSignal){this.startSignal=startSignal;this.doneSignal=doneSignal;idx=count++;}publicvoidrun(){try{System.out.println("the runnable is begin to init:"+idx);startSignal.await();System.out.println("the runnable is begin to start:"+idx);Thread.sleep(2000);doneSignal.countDown();System.out.println("the runnable is finish:"+idx);}catch(InterruptedExceptionex){}// return;}}

日志显示:

the runnable is begin to init:0 the runnable is begin to init:2 the runnable is begin to init:1 the runnables are inited the runnables are free to start the runnable is begin to start:2 the runnable is begin to start:1 the runnable is begin to start:0 the runnable is finish:1 the runnable is finish:0 the runnable is finish:2
http://www.jsqmd.com/news/1287803/

相关文章:

  • AI推理优化避坑指南——投机采样失败到KV Cache溢出的调优陷阱
  • PUBG罗技鼠标宏自动识别压枪:智能压枪终极指南
  • AI智能体测试
  • 2026 抖音动图去水印免费方法怎么用?高清原画质保留教程 - 工具软件使用方法推荐
  • VMware Workstation 17 Pro 虚拟机安装与配置全攻略
  • 2026大理短租别墅哪里靠谱 熹庭Villas获文旅局认证 - 汇聚至此
  • 2026德州黄金回收就来丽坤奢品汇18617962974全国连锁专业靠谱 - 丽坤奢品汇
  • 3步告别混乱:BG3ModManager模组管理全攻略
  • 义乌火锅探店|被福田禧牛潮汕鲜牛火锅狠狠圈粉,好吃不踩雷 - 一知资讯
  • 开源项目的许可证选型总结:MIT、AGPL 与 BSL 的真实选择经验
  • MX 暑假集训 7.29
  • 如何快速完整备份QQ空间历史说说?GetQzonehistory三步搞定
  • 5分钟终极指南:为苹果触控板安装专业级Windows驱动,实现完美触控体验
  • 2026保姆级教程:PDF转PNG怎么操作?多款工具手把手教你 - 工具软件使用方法推荐
  • Ubuntu远程连接Windows:RDP、VNC与SSH隧道方案详解
  • 弹性GPU算力平台怎么选?从万卡智算到自动扩缩容的全维度选型指
  • 硬核改造:用示波器XY模式运行《雷神之锤》的嵌入式图形实践
  • 深度掌控AMD Ryzen性能:SMUDebugTool硬件级调试与优化实战指南
  • AWR1443毫米波雷达单芯片方案:近距离感知的设计与实现
  • 海口本地黄金回收门店盘点!30 年老品牌,极速上门当场结款 - 一日一测评
  • 如何用pyfolio三步完成专业级投资组合分析:完整实战指南
  • 行空板K10中文显示实战:基于LVGL字体集成与子集化方案
  • Path of Building:流放之路离线角色构建模拟器的完整使用指南
  • TI SimpleLink芯片供应商认证与OTP配置实战指南
  • 轻奢风格大理短租别墅选购指南:如何挑选高品质度假住所 - 汇聚至此
  • C++类从入门到精通:封装、构造、静态成员与友元全解析
  • .NET Windows Desktop Runtime:五分钟快速上手的终极部署指南
  • 自适应海洋捕食者算法优化与MATLAB实现
  • Mac Mouse Fix 3.0:重新定义macOS鼠标体验的5个关键突破
  • 从选题到发布,新手做自媒体必装的6大Skill