CountDownLatch

CountDownLatch 针对有强先后关系类的并发顺序,而不是允许随机的情况。

CountDownLatch其实可以把它看作一个计数器,只不过这个计数器的操作是原子操作,同时只能有一个线程去操作这个计数器,也就是同时只能有一个线程去减这个计数器里面的值。
你可以向CountDownLatch对象设置一个初始的数字作为计数值,任何调用这个对象上的await()方法都会阻塞,直到这个计数器的计数值被其他的线程减为0为止。
CountDownLatch的一个非常典型的应用场景是:有一个任务想要往下执行,但必须要等到其他的任务执行完毕后才可以继续往下执行。假如我们这个想要继续往下执行的任务调用一个CountDownLatch对象的await()方法,其他的任务执行完自己的任务后调用同一个CountDownLatch对象上的countDown()方法,这个调用await()方法的任务将一直阻塞等待,直到这个CountDownLatch对象的计数值减到0为止。

运动员示例:

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by bingone on 15/12/28.
 */
public class Test {
    public static CountDownLatch playGround = new CountDownLatch(1);
    public static Random random = new Random();
    public static ExecutorService executorService = Executors.newCachedThreadPool();
    public static CountDownLatch begin;
    public static CountDownLatch end;
    public static int num = 5;
    public static void main(String... args) throws IOException {
        executorService.submit(new judge("Tom", num));
        for(int i = 0;i<num;i++){
            final String name = "num"+i;
            executorService.submit(new Runnable() {
                String name;
                {
                    name="asfd";
                }
                public Runnable test(){
                    System.out.println("construct");
                    return this;
                }
                @Override public void run() {
                    System.out.println("player " + name + " await ...");
                    try {
                        begin.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("player " + name + " running~~~");
                    try {
                        Thread.sleep(random.nextInt(10)*1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("player " + name + " arrive final");
                    end.countDown();
                }
            }.test());
        }

        System.out.println("playground haven't ready");
        playGround.countDown();
        executorService.shutdown();
        while(!executorService.isTerminated())
            Thread.yield();
        System.out.println("all is over");

    }

    public static final class judge implements Runnable {

        String name;
        public judge(String name, int player) {
            this.name = name;
            begin = new CountDownLatch(1);
            end = new CountDownLatch(player);
        }

        @Override public void run() {
            System.out.println("judge " + name + " come in playground");
            try {
                System.out.println("judge " + name + " await playground");
                playGround.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("judge blow a whistle");
            begin.countDown();
            try {
                System.out.println("judge " + name + " await players");
                end.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("judge " + name + "says end");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值