浅析Java中CountDownLatch用法

23 篇文章 0 订阅

CountDownLatch如其所写,是一个倒计数的锁存器,当计数减至0时触发特定的事件。利用这种特性,可以让主线程等待子线程的结束。下面以一个模拟运动员比赛的例子加以说明。

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.util.Log;

public class CountDownLatchDemo {
	private static final int PLAYER_AMOUNT = 5;
	public CountDownLatchDemo() {
		// TODO Auto-generated constructor stub
	}
	/**
	* @param args
	*/
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//对于每位运动员,CountDownLatch减1后即结束比赛
		CountDownLatch begin = new CountDownLatch(1);
		//对于整个比赛,所有运动员结束后才算结束
		CountDownLatch end = new CountDownLatch(PLAYER_AMOUNT);
		Player[] plays = new Player[PLAYER_AMOUNT];

		for(int i=0; i<PLAYER_AMOUNT; i++)
			plays[i] = new Player(i+1, begin, end);

		//设置特定的线程池,大小为5
		ExecutorService exe = Executors.newFixedThreadPool(PLAYER_AMOUNT);
		for(Player p:plays)
			exe.execute(p);            //分配线程

		System.out.println("Race begins!");
		Log.i("countdownlatch", "Race begins!");
		begin.countDown();
		try {
			end.await();            //等待end状态变为0,即为比赛结束
		} catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			System.out.println("Race ends!");
			Log.i("countdownlatch", "Race ends!");
		}
		exe.shutdown();
	}
}
class Player implements Runnable {

	private int id;
	private CountDownLatch begin;
	private CountDownLatch end;
	public Player(int i, CountDownLatch begin, CountDownLatch end) {
		// TODO Auto-generated constructor stub
		super();
		this.id = i;
		this.begin = begin;
		this.end = end;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			begin.await(); //等待begin的状态为0
			Thread.sleep((long)(Math.random()*100)); //随机分配时间,即运动员完成时间
			System.out.println("Play"+id+" arrived.");
			Log.i("countdownlatch", "Play"+id+" arrived.");
		} catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			end.countDown(); //使end状态减1,最终减至0
		}
	}
}

原文地址


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值