【JUC】JUC's powerful auxiliary class

Decrement count CountDownLatch

The await method of the CountDownLatch object is blocking, and the count is 0 to pass. countDown() is not blocking and will decrement the count by one

Example: The monitor can only lock the door after 6 students have left the classroom

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

        //创建CountDownLatch对象,设置初始值
        CountDownLatch countDownLatch = new CountDownLatch(6);

        //6个同学陆续离开教室
        for(int i = 1; i <= 6; i++) {
    
    
            new Thread(()->{
    
    
                countDownLatch.countDown();
                System.out.println(Thread.currentThread().getName() + "号同学离开了教室");
            }, String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "班长锁门走了");
    }
}

CyclicBarrier

Example: Summon Shenlong with its seven Dragon Balls (continue to execute)

public class CyclicBarrierDemo {
    
    

    //创建固定在
    private static final int NUMBER = 7;
    public static void main(String[] args) {
    
    

        //创建CyclicBarrier, 设定固定值,及达到固定在要做什么
        CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER, ()->{
    
    
            System.out.println("集齐了七颗龙珠,召唤神龙");
        });

        //集齐7颗龙珠的过程
        for (int i = 1; i <= 7; i++) {
    
    
            new Thread(()->{
    
    
                try {
    
    
                    System.out.println(Thread.currentThread().getName() + "星龙珠被收集到了");
                    cyclicBarrier.await();//没有达到7的时候,不会执行await之后的语句,线程会阻塞,达到7时放行,并执行cyclicBarrier的runnable
                    System.out.println("哈哈哈");
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();
        }
    }
}

Semaphore

acquire() method, acquire a license from this semaphore, the thread will block
release() until a license is provided, release a license, and return it to the semaphore

Example: Six cars, parked in three spaces

/**
 * 六辆汽车,停在三个停车位
 */
public class SemaphoreDemo {
    
    
    public static void main(String[] args) {
    
    
        Semaphore semaphore = new Semaphore(3);

        //模拟6辆车
        for (int i = 1; i <= 6; i++) {
    
    
            new Thread(() ->{
    
    
                try {
    
    
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + "抢到了停车位");

                    //设置随机停车时间
                    TimeUnit.SECONDS.sleep(new Random().nextInt(5));
                    System.out.println(Thread.currentThread().getName() + "------离开了停车位");

                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }finally {
    
    
                    semaphore.release();
                }
            }, String.valueOf(i)).start();
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_44179010/article/details/123365333