zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    python中selenium+unittest实操
    python+selenium元素定位04——浏览器多窗口处理
    python+selenium元素定位03——自动化常见场景处理
    python+selenium元素定位02——层级定位
    requests.post() 方法的使用
    python+selenium元素定位01——显式、隐式等待
    python+selenium之元素识别二
    IO流常用基类
    STS中导入Jmeter源码遇到的坑
    MySql处理日期时间常用函数
  • 原文地址:https://www.cnblogs.com/nayitian/p/3317295.html
Copyright © 2011-2022 走看看