Java:多线程,CountDownLatch同步器
1. 背景
CountDownLatch类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
用给定的计数 初始化 CountDownLatch。由于调用了 countDown() 方法,所以在当前计数到达零之前,await 方法会一直受阻塞。之后,会释放所有等待的线程,await 的所有后续调用都将立即返回。这种现象只出现一次——计数无法被重置。
2. 示范代码
下面代码示范了如果扑克游戏一桌人都到齐了,则翻底牌:
package com.clzhang.sample.thread; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SyncCountDownLatch { class PokerPlayer implements Runnable { private final String[] POINTS = new String[]{"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",}; private String name; private CountDownLatch countDown; public PokerPlayer(String name, CountDownLatch countDown) { this.name = name; this.countDown = countDown; } @Override public void run() { try { Thread.sleep((long) (Math.random() * 5000)); // 随机抽一张牌 Random random = new Random(); String myPoint = POINTS[random.nextInt(13)]; System.out.println(name + "ready!"); // 准备就绪,等待其它玩家也就绪 countDown.countDown(); countDown.await(); // 玩家都就绪了,翻底牌 System.out.println(name + ":" + myPoint); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { final int PER_TABLE_PLAYERS = 4; // 多少人够开一桌的
CountDownLatch countDown = new CountDownLatch(PER_TABLE_PLAYERS); SyncCountDownLatch ins = new SyncCountDownLatch(); ExecutorService executorPool = Executors.newFixedThreadPool(PER_TABLE_PLAYERS); for(int i=0; i<PER_TABLE_PLAYERS; i++) { executorPool.execute(ins.new PokerPlayer(i +"号玩家", countDown)); } executorPool.shutdown(); } }
输出
3号玩家ready!
0号玩家ready!
1号玩家ready!
2号玩家ready!
3号玩家:5
0号玩家:3
1号玩家:7
2号玩家:6