- 浏览: 797232 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
huan19900606:
像haskell这些脚本语言很容易定义DSL,实现对应的词法语 ...
DSL的基本介绍(groovy来进行构建) -
express_wind:
https://yq.aliyun.com/album/130 ...
qlexpress规则引擎初探 -
readxuxuegang:
博主你好。如果groovy的代码是保存在数据库里,不是文件,这 ...
在java中使用groovy怎么搞 (java and groovy) -
express_wind:
hi,兄弟,有没有兴趣来阿里巴巴专门做这方面的研究,https ...
qlexpress规则引擎初探 -
langcaiye:
有2个问题请教:1. 这里的base32算法为什么需要以负数的 ...
【原】geohash算法详解
最近遇到一个问题,需要批量的从数据库中读取数据,但是对于数据的处理需要等待一部分完成之后再进行下一批的数据。抽象出来就是一个任务,分成N份来执行,每一份由多个线程去执行,一份完成之后再执行下一份。于是想起了CountDownLatch。
网上关于CountDownLatch的文章很多,搜了一些,也大体看了一下,但是感觉最好的还是CountDownLatch官方代码里面的两个例子,现在把这两个例子简单说明一下。
CountDownLatch一个线程同步的工具,是的一个或者多个线程等待其他线程操作完成之后再执行。
CountDownLatch通过一个给定的数值count来进行初始化,方法await()一直阻塞直到当前的count到达零为止,count的数值通过countDown()方法来减1,count的数值一旦设定就不能再修改,如果需要进行修改,请考虑使用CyclicBarrier。
大体看了一下源代码,是通过同步队列来作为计数器来进行控制的。同步队列是在CountDownLatch内部实现了一个静态内部类,countDown()通过调用队列来减1。
有两个典型的应用场景:
第一种是一个开始的信号,所有的task任务等待这个信号。类似于百米赛跑中的信号员,所有的运动员都做好准备,等待信号,信号一来,那就开始运行。
第二种是将一个任务分支N部分由M个线程来处理,等待所有的线程M完成任务后做其他的事情,还是刚才的例子,所有运动员跑完之后,才能知道所有人员的排名情况。
public class CountDownLatchTest { public static void main(String[] args) throws Exception{ CountDownLatch s = new CountDownLatch(1); CountDownLatch e = new CountDownLatch(6); for(int i=0;i<6;i++){ new Thread(new Worker(s,e)).start(); } System.out.println("i am the judge ,now ,i start the singal"); s.countDown(); System.out.println("waiting all task over"+e.getCount()); e.await(); System.out.println("all is over"); } } class Worker implements Runnable{ private final CountDownLatch startSingal ; private final CountDownLatch endSingal; public Worker(CountDownLatch startSingal, CountDownLatch endSingal) { super(); this.startSingal = startSingal; this.endSingal = endSingal; } public void run() { try { System.out.println(Thread.currentThread().getName()+"waiting the start singal...."+startSingal.getCount()); //等待开始信号信号 startSingal.await(); System.out.println(Thread.currentThread().getName()+"start to executer"); //结束的计数器减一 endSingal.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }
执行结果:
Thread-0waiting the start singal....1
Thread-2waiting the start singal....1
Thread-1waiting the start singal....1
i am the judge ,now ,i start the singal
waiting all task over6
Thread-1start to executer
Thread-3waiting the start singal....1
Thread-4waiting the start singal....1
Thread-3start to executer
Thread-5waiting the start singal....0
Thread-5start to executer
Thread-2start to executer
Thread-0start to executer
Thread-4start to executer
all is over
public static void main(String[] args) throws Exception{ CountDownLatch latch = new CountDownLatch(6); Executor e = Executors.newFixedThreadPool(6); System.out.println("thread number is 6,now start"); for(int i=0;i<6;i++){ e.execute(new Worker(latch,i)); } System.out.println("waiting all is over "); latch.await(); System.out.println("all is over"); } } class Worker implements Runnable{ private final CountDownLatch number; private int temp; public Worker(CountDownLatch number, int temp) { super(); this.number = number; this.temp = temp; } public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) {} System.out.println(Thread.currentThread().getName()+"runnable - "+temp); number.countDown(); } }
写道
thread number is 6,now start
waiting all is over
pool-1-thread-4runnable - 3
pool-1-thread-5runnable - 4
pool-1-thread-1runnable - 0
pool-1-thread-3runnable - 2
pool-1-thread-2runnable - 1
pool-1-thread-6runnable - 5
all is over
waiting all is over
pool-1-thread-4runnable - 3
pool-1-thread-5runnable - 4
pool-1-thread-1runnable - 0
pool-1-thread-3runnable - 2
pool-1-thread-2runnable - 1
pool-1-thread-6runnable - 5
all is over
评论
1 楼
TrueBrian
2015-03-17
|
发表评论
-
系统分布式情况下最终一致性方案梳理
2015-09-05 19:34 40808前言 目前的应用系 ... -
Storm核心概念剖析
2015-03-20 20:42 3179最近团队中有分析的场 ... -
池和流的两种数据处理方式
2014-11-19 22:59 1322在抽象层面,想了一下,目前很多的数据处理形式,一般分为池和流 ... -
关于CodeReview(java)
2014-10-29 20:42 1837关于codereview,在平时的开发中,经常忽略的环节,参 ... -
java中各种各样的数据结构
2014-07-13 20:26 2399在java中,有非常丰富的数据结构,可能是因为大多数的软件 ... -
关于JVM的ClassLoader(笔记)
2014-07-13 12:19 1795众所周知,java是编译型的语言,写的是java文 ... -
关于事务的几个概念介绍
2014-06-06 22:22 1893啥是事务? 有一组操 ... -
开发中遇到的编码问题
2014-05-22 19:39 17941、说到编码,最大的问题就是乱码了,为啥会有乱码呢 ? 因 ... -
ThreadLocal源代码解析
2014-04-24 17:54 2359最开始的时候,理解的ThreadLocal,我的理解是这样的 ... -
关于单例模式(代码篇)
2014-04-23 10:47 2369很早的时候,转发过一篇单例模式的文章:http://iamz ... -
今天遇到的两个spring相关的两个问题
2014-04-18 21:56 2493今天在项目中写代码,遇到两个Spring的问题,记录一下。再 ... -
Activiti中的命令模式解析
2014-04-11 13:10 3088最近在看Activiti的源代码,发现是基于命令模式进行的开 ... -
关于java中的本地缓存-总结概述
2014-03-31 19:00 18291java中的本地缓存,工作后陆续用到,一直想写,一直无从下 ... -
使用guava中的EventBus构建内存级别的事件引擎
2014-03-25 19:27 6320这个EventBus是guava中比较给力的一个类,从字面 ... -
DSL的基本介绍(groovy来进行构建)
2014-03-04 23:32 16930什么是DSL? 领域特定 ... -
qlexpress规则引擎初探
2014-02-25 22:28 24861qlexpress是啥? 这个是阿里内部的一个开源的jav ... -
在java中使用groovy怎么搞 (java and groovy)
2014-01-15 23:17 10852什么是groovy? 一种基于Java虚拟机的动态语言, ... -
java中记录方法调用时间,结果按照方法的层级树状的输出
2013-12-21 17:36 4624 在java中,最常用的埋点时间的方法就 ... -
一次CMS GC问题排查过程(理解原理+读懂GC日志)
2013-12-14 22:21 41221这个是之前处理过的一个线上问题,处理过程断断续续,经历了两 ... -
令牌桶算法和漏桶算法以及流量控制浅谈
2013-11-27 23:20 20705 在双十一等大促环节,系统需要限流,外部 ...
相关推荐
递减锁存器CountDownLatch的使用以及注意事项!
java并发编程中CountDownLatch和CyclicBarrier的使用借鉴.pdf
《java并发编程》中CountDownLatch和CyclicBarrier用法实例大全,几乎包含了所有重要的用法
主要介绍了JAVA多线程CountDownLatch的使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
利用 CountDownLatch 类实现线程同步,而不用回调机制。详见我的博文 http://blog.csdn.net/kroclin/article/details/37956949
文章目录1 原理简介2 具体使用方法2.1 demo1 — await不传入时间,保证当前线程的其他操作在最后执行2.2 demo2 — await传入时间t,当前线程等其他线程时间t后就运行其他操作2.3 发令枪 源码地址:...
目录 CountDownLatch是什么? CountDownLatch如何工作? 在实时系统中的应用场景 应用范例 常见的面试题 代码样例
在网上找的一个CountDownLatch的学习demo,感觉很不错,就摘抄过来了
mybaits 多线程 实现数据批量插入 (运用CountDownLatch实现闭锁) 1、mybatis批处理 2、数据分批量查询 3、数据分批量插入
CountDownLatch与thread.join()的区别
java并发编程中CountDownLatch和CyclicBarrier的使用.pdf
NULL 博文链接:https://cpjsjxy.iteye.com/blog/2272451
CountDownLatch Demo
并发编程之CountDownLatch
Java并发编程一CountDownLatch、CyclicBarrier、Semaphore初使用 CountDownLatch、CyclicBarrier、Semaphore这些线程协作工具类是基于AQS的,看完这篇博客后可以去看下面这篇博客,了解它们是如何实现的。 Java并发...
主要为大家详细介绍了CountDownLatch的使用说明,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要介绍了如何使用CountDownLatch同步java多线程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
3.2 使用CountDownLatch实现同步 主线程等待多个线程完成 4.1 场景介绍 4.2 使用CountDownLatch实现等待 CountDownLatch的其他应用场景 5.1 倒计时计时器 5.2 同时开始任务 5.3 等待多个资源就绪 CountDownLatch与...
于是乎到现在的Hibernate、MyBatis、Spring、Spring MVC、AQS、ThreadPoolExecutor、CountDownLatch使用场景和核心源码分析。 感觉自己还是真的菜鸡,有太多框架的底层实现都不怎么了解。 当山头被一座一座攻克时,...
CountDownLatch是一个同步工具类,它通过一个计数器来实现的,初始值为线程的数量。每当一个线程完成了自己的任务,计数器的值就相应得减1。当计数器到达0时,表示所有的线程都已执行完毕,然后在等待的线程就可以恢复...