其他分享
首页 > 其他分享> > CountDownLatch demo演示裁判和选手赛跑

CountDownLatch demo演示裁判和选手赛跑

作者:互联网

# CountDownLatch demo演示裁判和选手赛跑
package com.example.core.mydemo;

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

/**
 * // 一个CountDouwnLatch实例是不能重复使用的,也就是说它是一次性的,锁一经被打开就不能再关闭使用了,如果想重复使用,请考虑使用CyclicBarrier。
 * 参考:https://www.cnblogs.com/ningxu/p/3372611.html
 * https://www.cnblogs.com/ningxu/p/3372502.html
 * https://www.cnblogs.com/ningxu/p/3372599.html
 */
public class CountDownLatchTest2 {
    // 模拟了100米赛跑,10名选手已经准备就绪,只等裁判一声令下。当所有人都到达终点时,比赛结束。
    public static void main(String[] args) throws InterruptedException {

        // 开始的倒数锁
        final CountDownLatch begin = new CountDownLatch(1);

        // 结束的倒数锁
        final CountDownLatch end = new CountDownLatch(10);

        // 十名选手
        final ExecutorService exec = Executors.newFixedThreadPool(10);

        for (int index = 0; index < 10; index++) {
            final int NO = index + 1;
            Runnable run = new Runnable() {
                public void run() {
                    try {
                        // 如果当前计数为零,则此方法立即返回。
                        // 等待
                        begin.await();
                        Thread.sleep((long) (Math.random() * 10000));
                        System.out.println("No." + NO + " arrived");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        // 每个选手到达终点时,end就减一
                        end.countDown();
                    }
                }
            };
            exec.submit(run);
        }
        System.out.println("Game Start");
        // begin减一,开始游戏
        begin.countDown();
        // 等待end变为0,即所有选手到达终点
        end.await();
        System.out.println("Game Over");
        exec.shutdown();
    }
}

 

标签:index,赛跑,end,demo,选手,CountDownLatch,com,final
来源: https://www.cnblogs.com/oktokeep/p/16615890.html