CountDownLatch & CyclicBarrier & Semaphore

CountDownLacth

CountDownLacth(倒计数锁存器)到底有什么用呢?我们来看下面这个场景。

我们又有小片片要开始拍摄了,这个小片片需要5个演员来演,开演之前,导演需要这5个演员全部准备好才能Action,5个演员听到导演叫Action就开演了。

使用CountDownLacth完成以上场景。

public class CountDownLatchDemo {
    private static final Random RANDOM = new Random();
    private static final String[] ACTORS ={"波多野结衣","吉泽明步","苍井空","小泽玛利亚","泷泽萝拉"};

    static class Actor implements Runnable{
        private String name ;
        private CountDownLatch readyLacth;
        private CountDownLatch startLacth;

        public Actor(String name,CountDownLatch readyLacth,CountDownLatch startLacth){
            this.name = name;
            this.readyLacth = readyLacth;
            this.startLacth = startLacth;
        }
        @Override
        public void run() {
            try {
                int readyTime = RANDOM.nextInt(1000);
                System.out.println(name + ":我需要" + readyTime/100 + "分钟时间准备.");
                //模拟准备时间
                Thread.sleep(readyTime);
                System.out.println(name + ":我已经准备好了!");
                readyLacth.countDown();
                //等待导演发令
                startLacth.await();
                System.out.println(name + ":我开始表演了,aaaaa,oooooooo,kimoji");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        //导演的Lacth,每个演员准备好,该Latch就-1,需要等待所有演员准备好
        CountDownLatch readyLacth = new CountDownLatch(ACTORS.length);
        //演员的Lacth,需要等待导演叫开始,该latch -1
        CountDownLatch startLacth = new CountDownLatch(1);

        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i=0;i<ACTORS.length;i++){
                Actor actor = new Actor(ACTORS[i],readyLacth,startLacth);
                executorService.execute(actor);
        }
        readyLacth.await();
        startLacth.countDown();
        System.out.println("导演 : Action!");
        executorService.shutdown();
    }

}
波多野结衣:我需要5分钟时间准备.
吉泽明步:我需要2分钟时间准备.
苍井空:我需要3分钟时间准备.
小泽玛利亚:我需要6分钟时间准备.
泷泽萝拉:我需要4分钟时间准备.
吉泽明步:我已经准备好了!
苍井空:我已经准备好了!
泷泽萝拉:我已经准备好了!
波多野结衣:我已经准备好了!
小泽玛利亚:我已经准备好了!
导演 : Action!
苍井空:我开始表演了,aaaaa,oooooooo,kimoji
小泽玛利亚:我开始表演了,aaaaa,oooooooo,kimoji
吉泽明步:我开始表演了,aaaaa,oooooooo,kimoji
波多野结衣:我开始表演了,aaaaa,oooooooo,kimoji
泷泽萝拉:我开始表演了,aaaaa,oooooooo,kimoji

CyclicBarrier

现在,我们要拍两个场景,把演员分成两组(人数一样),导演要指挥拍这两个场景。

使用CountDownLacth也可以完成这个需求,在第一个场景拍摄完毕后重设CountDownLacth。使用CyclicBarrier则可以重复使用。

使用CyclicBarrier完成如上需求。

public class CyclicBarrierDemo {
    private static final Random RANDOM = new Random();
    private static final String[] ACTORS1 ={"波多野结衣","吉泽明步","苍井空"};
    private static final String[] ACTORS2 ={"小泽玛利亚","泷泽萝拉","小川阿佐美"};

    static class Actor implements Runnable{
        private String name ;
        private CyclicBarrier barrier;

        public Actor(String name,CyclicBarrier barrier){
            this.name = name;
            this.barrier = barrier;
        }
        @Override
        public void run() {
            try {
                int readyTime = RANDOM.nextInt(1000);
                System.out.println(name + ":我需要" + readyTime/100 + "分钟时间准备.");
                //模拟准备时间
                Thread.sleep(readyTime);
                System.out.println(name + ":我已经准备好了!");
                //等待导演发令
                barrier.await();
                System.out.println(name + ":我开始表演了,aaaaa,oooooooo,kimoji");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier barrier = new CyclicBarrier(ACTORS1.length, new Runnable() {
            public void run() {
                System.out.println("导演:Action!");
            }
        });
        for (int i = 0; i < ACTORS1.length; i++) {
            Thread t = new Thread(new Actor(ACTORS1[i],barrier));
            t.start();
        }
        Thread.sleep(10000);
        System.out.println("==============更换场景==========================");
        for (int i = 0; i < ACTORS2.length; i++) {
            Thread t = new Thread(new Actor(ACTORS2[i],barrier));
            t.start();
        }
    }
}
波多野结衣:我需要4分钟时间准备.
吉泽明步:我需要0分钟时间准备.
苍井空:我需要6分钟时间准备.
吉泽明步:我已经准备好了!
波多野结衣:我已经准备好了!
苍井空:我已经准备好了!
导演:Action!
苍井空:我开始表演了,aaaaa,oooooooo,kimoji
吉泽明步:我开始表演了,aaaaa,oooooooo,kimoji
波多野结衣:我开始表演了,aaaaa,oooooooo,kimoji
==============更换场景==========================
小泽玛利亚:我需要6分钟时间准备.
泷泽萝拉:我需要1分钟时间准备.
小川阿佐美:我需要8分钟时间准备.
泷泽萝拉:我已经准备好了!
小泽玛利亚:我已经准备好了!
小川阿佐美:我已经准备好了!
导演:Action!
小川阿佐美:我开始表演了,aaaaa,oooooooo,kimoji
泷泽萝拉:我开始表演了,aaaaa,oooooooo,kimoji
小泽玛利亚:我开始表演了,aaaaa,oooooooo,kimoji

Semaphore

最近的女星有点懒惰,爱耍大牌,准备很久都准备不好,耽误时间。都是大牌儿,又不敢得罪,导演组决定,最先准备好的先拍,后面来的按顺序排队,等前面的杀青了再拍,不耽误时间。

这时候使用Semaphore就比较方便啦

public class SemaphoreDemo {
    private static final Random RANDOM = new Random();
    private static final String[] ACTORS ={"波多野结衣","吉泽明步","苍井空","小泽玛利亚","泷泽萝拉","小川阿佐美"};

    static class Actor implements Runnable{
        private String name ;
        private Semaphore semaphore;

        public Actor(String name,Semaphore semaphore){
            this.name = name;
            this.semaphore = semaphore;
        }
        @Override
        public void run() {
            try {
                int readyTime = RANDOM.nextInt(1000);
                System.out.println(name + ":我需要" + readyTime/100 + "分钟时间准备.");
                //模拟准备时间
                Thread.sleep(readyTime);
                if(semaphore.availablePermits()>0){
                    System.out.println(name + ":我已经准备好了!");
                }else{
                    System.out.println(name + ":我已经准备好了!啊,来晚了呢");
                }
                semaphore.acquire();
                System.out.println(name + ":我开始表演了,aaaaa,oooooooo,kimoji");
                Thread.sleep(1000);
                System.out.println(name + ":演完啦!");
                semaphore.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ExecutorService executorService= Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(3);
        for(int i=0;i<ACTORS.length;i++){
            Actor actor = new Actor(ACTORS[i],semaphore);
            executorService.execute(actor);
        }
        executorService.shutdown();
    }
}
吉泽明步:我需要6分钟时间准备.
波多野结衣:我需要9分钟时间准备.
苍井空:我需要1分钟时间准备.
小泽玛利亚:我需要4分钟时间准备.
泷泽萝拉:我需要5分钟时间准备.
小川阿佐美:我需要8分钟时间准备.
苍井空:我已经准备好了!
苍井空:我开始表演了,aaaaa,oooooooo,kimoji
小泽玛利亚:我已经准备好了!
小泽玛利亚:我开始表演了,aaaaa,oooooooo,kimoji
泷泽萝拉:我已经准备好了!
泷泽萝拉:我开始表演了,aaaaa,oooooooo,kimoji
吉泽明步:我已经准备好了!啊,来晚了呢
小川阿佐美:我已经准备好了!啊,来晚了呢
波多野结衣:我已经准备好了!啊,来晚了呢
苍井空:演完啦!
吉泽明步:我开始表演了,aaaaa,oooooooo,kimoji
小泽玛利亚:演完啦!
小川阿佐美:我开始表演了,aaaaa,oooooooo,kimoji
泷泽萝拉:演完啦!
波多野结衣:我开始表演了,aaaaa,oooooooo,kimoji
吉泽明步:演完啦!
小川阿佐美:演完啦!
波多野结衣:演完啦!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值