JUC重要辅助

CountDownLatch

减计数器,当计数为0时执行相应的任务,用于一系列线程执行完后再执行相应线程任务。

public class TestCountDownLatch {
    public static void main(String args[]) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(5);
//        public CountDownLatch(int count) {
//            if (count < 0) throw new IllegalArgumentException("count < 0");
//            this.sync = new Sync(count);
//        }
        for(int i=1;i<=5;i++){
            final int temp = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"从房间离开了");
                countDownLatch.countDown();//计数器一直减1减到0
            },i+"号").start();
        }
        try{
            countDownLatch.await();//计数器不为0等待
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        //计数器为0时执行任务
        System.out.println("房间门关闭");
    }
}

CyclicBarrier

加计数器,当计数达到阈值时执行相应任务且计数器重置,即每次达到阈值指定任务都会执行

public class TestCyclicBarrier {
    public static void main(String args[]){
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("集齐七龙珠,成功召唤神龙");
        });
//        public CyclicBarrier(int parties, Runnable barrierAction) {
//            if (parties <= 0) throw new IllegalArgumentException();
//            this.parties = parties;
//            this.count = parties;
//            this.barrierCommand = barrierAction;
//        }
        for(int i=1;i<=21;i++){
            final int temp = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"集齐"+(temp%7+1)+"星珠");
                try{
                    cyclicBarrier.await();
                    //等待达到阈值执行一次任务并重置计数器
                    //cyclicBarrier.getNumberWaiting();可以查看当前计数器计数位置
                }catch(InterruptedException e){
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },temp+"号").start();
        }
    }
}

Semaphore

计数信号量,常用于限制可以访问某些资源的线程数量

public class TestSemaphore{
    public static void main(String args[]){
        Semaphore semaphore = new Semaphore(5);//初始化许可信号量
//        public Semaphore(int permits) {
//            sync = new Semaphore.NonfairSync(permits);
//        }
        for(int i=1;i<=15;i++){
            final int temp = i;
            new Thread(()->{
                try{
                    semaphore.acquire();//获取许可证,可带参(int)表示一个线程占用的的许可证数
                    System.out.println(Thread.currentThread().getName()+"抢到了车位");
                    TimeUnit.SECONDS.sleep(10);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    semaphore.release();//释放信号量,可带参(int)表示一个线程释放的的许可证数
                    System.out.println(Thread.currentThread().getName()+"离开了车位");
                }
            },"车辆"+temp).start();
        }
    }
}
Last modification:September 6th, 2020 at 06:41 pm