JUC种常用的辅助类

常用的辅助类

在JUC并发编程里面,有一个常用的辅助类;
java.util.concurrent 包下的;

在这里插入图片描述

1:CountDownLatch

这是一个一次性的现象 - 计数无法重置。
在这里插入图片描述

package com.baidu.add;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {
    //相当与我们的减法操作

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 1; i <=6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"Get Out");
                countDownLatch.countDown(); //数量减一1
            },String.valueOf(i)).start();
        }

        countDownLatch.await(); //等待计数器为零 ,然后在往下执行;

        System.out.println("Close Door");
    }

}

原理:
countDownLatch.countDown(); //数量减一1
countDownLatch.await(); //等待计数器为零 ,然后在往下执行;
在这里插入图片描述

2:CyclicBarrier

释放之后重新使用。
在这里插入图片描述

package com.baidu.add;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {

    public static void main(String[] args) {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("召唤神龙成功!");
        });

        for (int i = 1; i <= 7; i++) {

            final int temp = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"收集"+temp+"个龙珠.");

                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

在这里插入图片描述

3:Semaphore

在这里插入图片描述

package com.baidu.add;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreDemo {

    public static void main(String[] args) {

        //线程数量,停车位.限流!!
        Semaphore semaphore = new Semaphore(3);

        for (int i = 1; i <= 6; i++) {
           new Thread(()->{
               //acquire()得到
               try {
                   semaphore.acquire();
                   System.out.println(Thread.currentThread().getName()+"抢到车位");
                   TimeUnit.SECONDS.sleep(2);
                   System.out.println(Thread.currentThread().getName()+"离开车位");
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }finally {
                   //release()释放
                   semaphore.release();
               }
           },String.valueOf(i)).start();
        }
    }
}

原理:
semaphore.acquire(); 获得;假设已经满了,等待,等待被释放为止;
semaphore.release(); 释放,会将当前的释放量释放+1;然后唤醒等待的线程。

作用
多个共享的资源互斥的使用!并发限流,控制最大的线程数!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值