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. 【Unity3D】【NGUI】UILabel

    原文:http://www.tasharen.com/forum/index.php?topic=6706.0 NGUI讨论群:333417608 概述 UILabel是用来显示文本的脚本,继承自UI ...

  2. 【Python】Python 基础知识

    数字和表达式 >>> 2+3 5 >>> 1.0/2.0 0.5 >>> 1.0//2.0 # // 0.0 >>> 1%2 # ...

  3. 14.4.3.6 Fine-tuning InnoDB Buffer Pool Flushing 微调 InnoDB Buffer Pool 刷新:

    14.4.3.6 Fine-tuning InnoDB Buffer Pool Flushing 微调 InnoDB Buffer Pool 刷新: innodb_flush_neighbors an ...

  4. dll导入导出资源文件查看工具 InspectExe

    InspectExe lets you explore and diagnose problems with Win32 applications. It is integrated directly ...

  5. 新的方法 (New Approach)¶

    第一章:简介 - ANSI Common Lisp 中文版 新的方法 (New Approach)¶ 本书的目标之一是不仅是教授 Lisp 语言,而是教授一种新的编程方法,这种方法因为有了 Lisp ...

  6. struts2 与 OGNL 表达式,jsp中 利用ognl 在valuestack中取值

    在Struts2中,一个请求在终于到达Action的方法之前,Action对象本身会被压入ValueStack(实际上就是放到ValueStack的CompoundRoot中),所以Action对象是 ...

  7. Apache htaccess 重写假设文件存在!

    假设文件 data/cache/index.html 存在.那么才重写. 否则使用默认的MVC 重写.by default.fu@foxmail.com RewriteEngine on Rewrit ...

  8. Mysql 导入导出数据结构及数据

    方式一: mysqldump -ukevin -P3306 --default-character-set=utf8 -p -h10.1.15.123 activity sign_in_user &g ...

  9. android水平循环滚动控件

    CycleScrollView.java package com.example.test; import android.content.Context; import android.graphi ...

  10. 超人学院Hadoop大数据技术资源分享

    超人学院Hadoop大数据技术资源分享 http://bbs.superwu.cn/forum.php?mod=viewthread&tid=807&fromuid=645 很多其它精 ...