CountDownLatch应用实战
jopen
9年前
1. CountDownLatch简介
一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。其本质就是一个共享锁。
他最主要的作用是用来同步java的线程。
主要有一下三个方法:
主要方法:
public CountDownLatch(int count); public void countDown(); public void await();
构造方法参数指定了计数的次数
countDown方法,当前线程调用此方法,则计数减一
await方法,调用此方法会一直阻塞当前线程,直到计时器的值为0
2. 实战应用的例子
一个模拟运动员比赛的例子,首先要等所有的运动员全部给准备好,然后发起“起跑指令”所有的运动员开始起跑,等所有的运动员跑完才结束本次笔试。
代码如下:
运动员线程代码:
package it_cast.day01; import java.util.concurrent.CountDownLatch; public class Player implements Runnable { private int id; private CountDownLatch begin; private CountDownLatch end; public Player(int id, CountDownLatch begin, CountDownLatch end) { this.id=id; this.begin=begin; this.end=end; } @Override public void run() { // TODO Auto-generated method stub try { System.out.println("Play" + id + "ready"); begin.await();// 所有的运动员在这准备者,当一声令下就开始跑 Thread.sleep((long) (Math.random() * 100));// 随机分配时间,即运动员完成时间 System.out.println("Play" + id + " arrived."); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { end.countDown();// 每个运动员跑完,使end状态减1,最终减至0 } } }
主代码:
package it_cast.day01; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CountDownLatchDemo { private static final int PLAYER_AMOUNT = 5; public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub //对于每位运动员,CountDownLatch减1后即结束比赛 CountDownLatch begin = new CountDownLatch(1); //对于整个比赛,所有运动员结束后才算结束 CountDownLatch end = new CountDownLatch(PLAYER_AMOUNT); Player [] plays=new Player[PLAYER_AMOUNT]; //初始化5个运动员 for (int i = 0; i < plays.length; i++) { plays[i]=new Player(i, begin, end); } //设置特定的线程池,大小为5 ExecutorService exe = Executors.newFixedThreadPool(PLAYER_AMOUNT); for(Player p:plays) exe.execute(p); Thread.sleep(1000); System.out.println("Race begins!"); begin.countDown(); end.await();; //等待end状态变为0,即为所有人都跑完了,即为比赛结束 System.out.println("Race ends!"); exe.shutdown(); } }
运行结果如下: