常用的辅助类

常用的辅助类

一、CountDownLath

在这里插入图片描述

//计数器
public class CountDownLatchDemo {
    
    

    public static void main(String[] args) throws InterruptedException {
    
    
        // 倒计时总数是6, 必须要执行任务的时候,再使用!
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 0; i < 6; i++) {
    
    
            new Thread(()->{
    
    
                System.out.println(Thread.currentThread().getName() + " GO out");
                countDownLatch.countDown();     //数量减1
            },String.valueOf(i)).start();
        }
        countDownLatch.await();// 等待计数器归零,唤醒该线程,然后再向下执行
        System.out.println("close Door");
    }
}

原理:

countDownLatch.countDown(); 数量-1

countDownLatch.await(); 等待计数器归0,再向下执行

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

二、CyclicBarrier

CyclicBarrier : 指定个数线程执行完毕再执行操作
在这里插入图片描述

加法计数器

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("召唤神龙成功");
        });

        for (int i = 1; i <= 7; i++) {
    
    
            int temp = i;
            // lambda 能操作到i么? 本质不能,需要final修饰int temp
            // 但是jdk1.8之后有优化,
            new Thread(()->{
    
    
                System.out.println(Thread.currentThread().getName()+"收集"+temp+"个龙珠");

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

        }
    }
}

三、Semaphore

Semaphore:信号量

在这里插入图片描述

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
//抢车位!
//6车---3个停车位置
public class SemaphoreDemo {
    
    
    public static void main(String[] args) {
    
    
        // 可使用的线程数量:停车位,限流!
        Semaphore semaphore = new Semaphore(3);

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

原理:

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

作用:多个共享资源互斥的使用!并发限流,控制最大的线程数
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43803285/article/details/115374958