watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

package org.rui.thread.newc;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; /**
* Latch 锁存器
* 新类库中的构件 countDownLatch
*
* @author lenovo
*
*/
class TaskPortion implements Runnable { private static int counter = 0;
private final int id = counter++;
private static Random rand = new Random(47); private final CountDownLatch latch; public TaskPortion(CountDownLatch latch) {
this.latch = latch;
} @Override
public void run() {
try {
doWork();
latch.countDown();
} catch (InterruptedException e) {
// acceptable way to exit
} } // 处理业务代码
public void doWork() throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(rand.nextInt(2000));
System.out.println(this + " 完毕");
} public String toString() {
return String.format("%1$-3d", id);
} } // waits on the countDownLatch
class WaitingTask implements Runnable {
private static int counter = 0;// 计数
private final int id = counter++;
private static Random rand = new Random(47); private final CountDownLatch latch; WaitingTask(CountDownLatch latch) {
this.latch = latch;
} @Override
public void run() {
try {
// 调用countDown()的任务在产生调用时并没有被堵塞。仅仅有对await的调用会被堵塞,直至计数值到达0
// 等待问题被解决的任务在这个锁存器上调用await(),将它们自已拦住,直至锁存器计数结束
latch.await();
System.out.println("latch 障碍被觉得 " + this);
} catch (InterruptedException e) {
System.out.println(this + " interrupted");
}
} public String toString() {
return String.format("waitingTask %1$-3d", id);
}
} /**
* TaskPortio将随机地休眠一段时间,以模拟这部分工作的完毕,而WaitingTask表示系统中等待的部分。它要等待到问题的初始部分成完为止。
* 全部的任务都使用了在main中定义同一个单一的counDownLacth
*
* @author lenovo
*
*/
public class CountDownLatchDemo {
static final int SIZE = 100; public static void main(String[] args) throws InterruptedException {
ExecutorService exec = Executors.newCachedThreadPool();
CountDownLatch latch = new CountDownLatch(SIZE); // 都必须共享一个countDownLatch对象
for (int i = 0; i < 10; i++) {
exec.execute(new WaitingTask(latch));// 这个要等待 latch上面的为0时才会运行
}
for (int i = 0; i < SIZE; i++) {
exec.execute(new TaskPortion(latch));
} // latch.await();
System.out.println("launched all tasks");
exec.shutdown();// quit when all task complete
} }
/**
output:
launched all tasks
43 完毕
95 完毕
99 完毕
36 完毕
94 完毕
11 完毕
....
12 完毕
1 完毕
27 完毕
98 完毕
13 完毕
72 完毕
71 完毕
2 完毕
45 完毕
92 完毕
31 完毕
14 完毕
17 完毕
6 完毕
97 完毕
....
80 完毕
....
56 完毕
85 完毕
61 完毕
30 完毕
....
3 完毕
93 完毕
81 完毕
78 完毕
73 完毕
44 完毕
82 完毕
49 完毕
64 完毕
83 完毕
16 完毕
latch 障碍被觉得 waitingTask 2
latch 障碍被觉得 waitingTask 0
latch 障碍被觉得 waitingTask 4
latch 障碍被觉得 waitingTask 1
latch 障碍被觉得 waitingTask 5
latch 障碍被觉得 waitingTask 3
latch 障碍被觉得 waitingTask 7
latch 障碍被觉得 waitingTask 6
latch 障碍被觉得 waitingTask 9
latch 障碍被觉得 waitingTask 8
*/

java 线程 新类库中的构件 countDownLatch 使用的更多相关文章

  1. Java多线程之新类库中的构件CountDownLatch

    使用CountDownLatch类 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); ...

  2. Java多线程之新类库中的构件CyclicBarrier

    1.类说明: 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时 Cycl ...

  3. Java多线程之新类库中的构件DelayQueue

    DelayQueue 是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走.这种队列是有序的,即队头对象的延迟到期时间最长.注意:不 ...

  4. Java多线程之新类库中的构件PriorityBlockingQueue

    package concurrent2; import java.util.ArrayList; import java.util.List; import java.util.Queue; impo ...

  5. Java线程新特征——Java并发库

    一.线程池   Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利.为了编写高效稳定 ...

  6. Java线程新特性--- Lock

    在Java5中,专门提供了锁对象,利用锁可以方便的实现资源的封锁,用来控制对竞争资源并发访问的控制,这些内容主要集中在java.util.concurrent.locks包下面,里面有三个重要的接口C ...

  7. java线程并发工具类CyclicBarrier、CountDownLatch及Semaphore

    一.CyclicBarrier   (原文链接:http://www.studyshare.cn/blog-front/blog/index ) 1.定义 CyclicBarrier是线程并发工具类之 ...

  8. Java: 线程池(ThreadPoolExecutor)中的参数说明

    最近在看<阿里巴巴Android开发手册>,里面有这样几句话: [强制]新建线程时,必须通过线程池提供(AsyncTask 或者ThreadPoolExecutor或者其他形式自定义的线程 ...

  9. Java多线程-新特性-线程池

    Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利.为了编写高效稳定可靠的多线程程序 ...

随机推荐

  1. NTP工作机制及时间同步的方法

    Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它能够使计算机对其server或时钟源做同步化,它能够提供高精准度的时间校正,且可用加密确认的方式来防止恶毒的协 ...

  2. MFC的消息反射机制

    1.消息反射解释: 父窗口将子窗口发给它的通知消息,首先反射回子窗口进行处理(即给子窗口一个机会,让子窗口处理此消息),这样通知消息就有机会能被子窗口自身进行处理. 2.MFC中引入消息反射的原因: ...

  3. 关于jquery文件上传插件 uploadify 3.1的使用

    要使用uplaodify3.1,自然要下载相应的包,下载地址http://www.uploadify.com/download/,这里有两种包,一个是基于flash,免费的,一个是基于html5,需要 ...

  4. hdu1573-X问题

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 中国剩余定理 #include<iostream> #include<cstdio> ...

  5. ural 1519 Formula 1

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1519 题目分类:插头dp 题意:求经过所有可行点的哈密顿回路的个数  * 不可走 . 可 ...

  6. TForm.ShowModal只是接管消息循环,禁止外部键盘和鼠标输入到别的窗口,但并不封锁其它窗口继续获取消息(比如WM_TIMER消息仍可被发送到别的窗口上)

    窗体上放一个TTimer,然后双击输入: procedure TForm1.Timer1Timer(Sender: TObject); var cvs: TCanvas; Rect: TRect; S ...

  7. YUV格式具体解释

    YUV是指亮度參量和色度參量分开表示的像素格式,而这样分开的优点就是不但能够避免相互干扰,还能够减少色度的採样率而不会对图像质量影响太大.YUV是一个比較笼统地说法,针对它的详细排列方式,能够分为非常 ...

  8. POJ 3267-The Cow Lexicon(DP)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8252   Accepted: 3888 D ...

  9. 【C#遗补】之Char.IsDigit和Char.IsNumber的区别

    原文:[C#遗补]之Char.IsDigit和Char.IsNumber的区别 Char中IsDigit和IsNumber的两个方法都是用来判断字符是否是数字的,那他们有什么区别 IsDigit    ...

  10. hdu2151(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2151 分析: DP.思路:全盘扫描.     i表示时间,l表示第几棵树,方程:     step[i ...