• JUC强大的辅助类


    一、JUC强大的辅助类

    在这里插入图片描述

    1.CountDownLatch(减少计数)

    在这里插入图片描述

    CountDownLatch 记录线程个数
    CountDownLatch.countDown() 线程数减-1
    CountDownLatch.awit() 知道线程的个数为0的时候,就不等待了
    直到6个人全部离开教师之后,班长锁门

    import java.util.concurrent.CountDownLatch;
    
    public class CountDownLatchDemo {
        public static void main(String[] args) throws InterruptedException {
            //直到6个人全部离开教师之后,班长锁门
            CountDownLatch countDownLatch = new CountDownLatch(6);
    
            //循环
            for (int i = 1; i <= 6; i++) {
                new Thread(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 号同学离开教师");
                    //减少数据
                    countDownLatch.countDown();
                },String.valueOf(i)).start();
            }
    
            //如果初始化不为0的话,那么应该等待
            countDownLatch.await();
            //如果走完了,班长关门
            System.out.println(Thread.currentThread().getName()+"\t 班长关门走人");
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2.CyclicBarrier(循环栅栏)

    在这里插入图片描述

    CyclicBarrier 循环栅栏
    cyclicBarrier.await(); // 如果没有满足初始化值的时候,那么就会等待!

    集齐7颗龙珠,召唤神龙

    import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.CyclicBarrier;
    
    public class CyclicBarrierDemo {
        public static void main(String[] args) {
            //集齐7颗龙珠,召唤神龙
            CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
                System.out.println("集齐7颗龙珠,可以召唤神龙");
            });
    
            for (int i = 1; i <= 7 ; i++) {
                new Thread(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 星龙珠被收集 ");
                    try {
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } catch (BrokenBarrierException e) {
                        throw new RuntimeException(e);
                    }
                },String.valueOf(i)).start();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    3.Semaphore(信号灯)

    在这里插入图片描述

    semaphore.acquire(); 获取资源
    semaphore.release(); 释放资源

    三个停车场,停6辆汽车

    import java.util.concurrent.Semaphore;
    
    public class SemaphoreDemo {
        //三个停车场,停6辆汽车
        public static void main(String[] args) {
            //设置3个停车场
            Semaphore semaphore = new Semaphore(3);
            //模拟6辆汽车
            for (int i = 1; i <= 6 ; i++) {
                //创建一个线程
                new Thread(()->{
                    try {
                        //获取资源  获得车位
                        semaphore.acquire();
                        System.out.println(Thread.currentThread().getName()+"\t 抢到了车位");
                        Thread.sleep(1000);
                        System.out.println(Thread.currentThread().getName()+"\t------- 离开");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }finally {
                        //释放资源 让出车位
                        semaphore.release();
                    }
    
                },String.valueOf(i)).start();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    【C++右值引用】左右值的交叉引用的具体情景,右值详讲
    9.表示学习—Word2Vec
    springboot和springcloud的区别是什么?
    elasticsearch-head浏览器(google)插件安装使用
    c# 自定义分表
    Vue项目
    C++对象模型探索--02对象
    【OpenCV实战】3.OpenCV颜色空间实战
    Xiaojie雷达之路---脉冲压缩
    八股文随笔1
  • 原文地址:https://blog.csdn.net/qq_46548855/article/details/125960296