CountDownLatch、CyclicBarrier及Semaphore的用法示例

一、参考blog

https://www.cnblogs.com/dolphin0520/p/3920397.html

二、CountDownLatch

个人把它类比于一个持有计数的闸门,每到达这个闸门一个线程,计数减1,当计数为0时再执行闸门后续的动作。同时闸门失效了(只能用一次)。

public static void main(String[] args) {
        Executor fix = Executors.newFixedThreadPool(5);
        CountDownLatch latch = new CountDownLatch(2);
        Runnable runner = () -> {
            System.out.println("[" + Thread.currentThread().getName() + "]" + " started");
            System.out.println("[" + Thread.currentThread().getName() + "]" + " finished");
            latch.countDown();
        };
        fix.execute(runner);
        fix.execute(runner);
        try {
            System.out.println("[" + Thread.currentThread().getName() + "]" + " waiting on latch...");
            latch.await();
            System.out.println("[" + Thread.currentThread().getName() + "]" + " ok, i can do my work");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

 三、CyclicBarrier

用法差不多,特点:可以重复使用。

    public static void main(String[] args) {
        Executor fix = Executors.newFixedThreadPool(5);
        CyclicBarrier barrier = new CyclicBarrier(2);
        Runnable runner = () -> {
            try {
                Thread.sleep(new Random().nextInt(10000));
                System.out.println("[" + Thread.currentThread().getName() + "]" + " wait on barri " + new Date());
                barrier.await();
                System.out.println("[" + Thread.currentThread().getName() + "]" + " condition ok, do my work " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        };
        fix.execute(runner);
        fix.execute(runner);
    }

四、Semaphore

 向量,有点像锁。

import java.util.Date;
import java.util.Random;
import java.util.concurrent.*;

public class Test {
    //    
    public static void main(String[] args) {
        Executor fix = Executors.newFixedThreadPool(5);
        Semaphore semaphore = new Semaphore(2);

        Runnable runner = () -> {
            try {
                semaphore.acquire();
                System.out.println("[" + Thread.currentThread().getName() + "]" + " get a semaphore " + new Date());
                Thread.sleep(2000);
                System.out.println("[" + Thread.currentThread().getName() + "]" + " release a semaphore " + new Date());
                semaphore.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < 5; i++) {
            fix.execute(runner);
        }

    }

 

转载于:https://www.cnblogs.com/yoyotl/p/10084337.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值