• 常用辅助类


    CountDownLatch

            应用场景:1.多线程任务汇总。2.多线程任务阻塞住,等待发令枪响,一起执行。

     

      减法计数器

            每次有线程调用,数量-1,当计数器归零,countDownLatch.await()就会被唤醒向下执行。

    1. import java.util.concurrent.CountDownLatch;
    2. // 计数器
    3. public class CountDownLatchDemo {
    4. public static void main(String[] args) throws InterruptedException {
    5. //总数是6 必须要执行任务的时候再使用
    6. CountDownLatch countDownLatch = new CountDownLatch(6);
    7. for (int i = 0; i < 6; i++) {
    8. new Thread(()->{
    9. System.out.println(Thread.currentThread().getName() + " Go out");
    10. countDownLatch.countDown();//数量-1
    11. },String.valueOf(i)).start();
    12. }
    13. countDownLatch.await();
    14. System.out.println("Close Door");
    15. }
    16. }

     

    原理:

            countDownLatch.countDown(); //数量-1

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

            每次线程调用countDown()数量-1,假设计数器变成0,countDownLatch.await()就会被唤醒,继续执行!

    CyclicBarrier

            应用场景:比如LOL类游戏,满10人一组,开始游戏。

            加法计算器

    1. import java.util.concurrent.BrokenBarrierException;
    2. import java.util.concurrent.CyclicBarrier;
    3. public class CyclicBarrierDemo {
    4. public static void main(String[] args) {
    5. //集齐七颗龙珠召唤神龙
    6. CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
    7. System.out.println("召唤神龙成功!");
    8. });
    9. for (int i = 1; i <= 7; i++) {
    10. final int temp = i;
    11. new Thread(()->{
    12. System.out.println(Thread.currentThread().getName() + "收集了第" + temp + "颗龙珠");
    13. try {
    14. cyclicBarrier.await();
    15. } catch (InterruptedException e) {
    16. e.printStackTrace();
    17. } catch (BrokenBarrierException e) {
    18. e.printStackTrace();
    19. }
    20. }).start();
    21. }
    22. }
    23. }

     

    Semaphore

            Semaphore:信号量

     

            抢车位(6辆车,3个停车位)

    1. import java.util.concurrent.Semaphore;
    2. import java.util.concurrent.TimeUnit;
    3. public class SemaphoreDemo {
    4. public static void main(String[] args) {
    5. //线程数量 比作 停车位
    6. Semaphore semaphore = new Semaphore(3);
    7. for (int i = 1; i <= 6; i++) {
    8. new Thread(()->{
    9. //acquire() 得到
    10. try {
    11. semaphore.acquire();
    12. System.out.println(Thread.currentThread().getName() + "抢到车位");
    13. TimeUnit.SECONDS.sleep(2);
    14. System.out.println(Thread.currentThread().getName() + "离开车位");
    15. } catch (InterruptedException e) {
    16. e.printStackTrace();
    17. }finally {
    18. //release() 释放
    19. semaphore.release();
    20. }
    21. },String.valueOf(i)).start();
    22. }
    23. }
    24. }

     

    原理:

            semaphore.acquire(); //获得,假设已经满了,等待,等待被释放为止!

            semaphore.release();//释放,会将当前的信号量释放,然后唤醒等待线程!

    作用:

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

  • 相关阅读:
    [PAT练级笔记] 16 Basic Level 1016
    Web 性能优化:TLS
    一文了解 Python 迭代器介绍及其作用
    LINUX
    常用工具类之spring-boot-devtools热部署
    C# - this 的用法
    数据降维算法isomap mds
    回溯算法总结
    linux获取文件的属性
    Windows用户、组的管理
  • 原文地址:https://blog.csdn.net/weixin_48426115/article/details/128045542