Java并发-类库新组件 - CountDownLatch 理解

</pre><pre name="code" class="java">
直接附加上练习代码
package com.xyw.concurrent.blog;

import java.util.concurrent.*;
import java.util.*;

/*
 * CountDownLatch 是进行同步一个或者多个任务,强制他们等待由其他任务执行的一组操作完成。
 * 其有个初始的计数值,wait 的值都将被阻塞,直到计数值达到零的情况。 并且CountDownLatch 只能进行一次触发。计数器不能被重置。
 * 如果被重置,请看下一期的讲解的情况。
 */
class TaskPortion implements Runnable{
	private  static int counter = 0;
	private final int id = counter++; // 进行设置自增的ID 的选项
	private static Random rand = new Random(47);
	private final CountDownLatch latch;
	TaskPortion(CountDownLatch latch){
		this.latch = latch;
	}
	public void run(){
		try{
			doWork();
			latch.countDown();
		}catch(InterruptedException ex){
			
		}
	}
	public void doWork() throws InterruptedException{
		TimeUnit.MILLISECONDS.sleep(rand.nextInt(2000));
		System.out.println(this + "completed!");
	}
	public String toString(){
		return String.format("%1$-3d",	id);
	}
}// 这是个进行分开执行的任务, performs some portion of a task

class WaitingTask implements Runnable{
	private static int counter = 0;
	private final int id = counter++;
	private final CountDownLatch latch;
	WaitingTask(CountDownLatch latch){
		this.latch = latch;
	}
	public void run(){
		try{
			latch.await(); // 进行等待,知道 计数值为 零的时候,发生其中的停止的情况
			System.out.println("Latch barrier passed for " + this);
			//System.out.println(latch.getCount()); // 这个是用来进行监视,是不是其中运行的关键值得情况。
		}catch(InterruptedException e){
			System.out.println(this + " interrupted");
		}
	}
	public String toString(){
		return String.format("WaitingTask %1$-3d", id);
	}
}

public class CountDownLatchDemo {
	static final int SIZE = 10; // 你可以进行改变 SIZE 的值来进行了解 CountDownLatch 类的原理的情况
	public static void main(String[] args) throws Exception{
		ExecutorService exec = Executors.newCachedThreadPool();
		CountDownLatch latch = new CountDownLatch(SIZE);
		for(int i = 0; i< 100; i++){
			exec.execute(new WaitingTask(latch));
		}
		for(int i = 0; i < SIZE; i++){
			exec.execute(new TaskPortion(latch)); // 这些都是线程的进行的工作,并且只有
			// 其中的情况,等于一百的时候, WaitingTask 才进行运行,注意其中线程的先后的关系不是唯一确定的情况。
		}
		System.out.println("Launched all tasks");
		exec.shutdown(); // 当所有任务结束时完成
		System.out.print(latch.getCount()+ "\n");
	}
}
// 运行结果
<pre name="code" class="java">Launched all tasks
10
7  completed!
9  completed!
5  completed!
8  completed!
1  completed!
2  completed!
6  completed!
4  completed!
0  completed!
3  completed!
Latch barrier passed for WaitingTask 6  
Latch barrier passed for WaitingTask 2  
Latch barrier passed for WaitingTask 9  
Latch barrier passed for WaitingTask 0  
Latch barrier passed for WaitingTask 4  
Latch barrier passed for WaitingTask 5  
Latch barrier passed for WaitingTask 8  
Latch barrier passed for WaitingTask 3  
Latch barrier passed for WaitingTask 7  
Latch barrier passed for WaitingTask 1  


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值