Commonly used auxiliary classes in juc concurrent programming (8)

8. Commonly used auxiliary classes

1.CountDownLatch subtraction counter

package com.xizi.assistClass;

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()+"  Go Out");
                countDownLatch.countDown();//数量减一
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println("close Door");
    }
}

countDownLatch.ccountDown() count-1

countDownLatch.await(); //Wait for the counter to zero and then execute down

The number of countDown() calls -1 each time

Assuming that the counter becomes 0, countDownLatch.await() will be awakened to continue execution

 

2. CyclicBarrier addition counter

package com.xizi.assistClass;

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++) {
            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 semaphore

package com.xizi.assistClass;

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 {
                    semaphore.release();//释放
                }
            },String.valueOf(i)).start();
        }

    }
}

acquire(); If the acquisition hypothesis is full, wait until it is released

release(); release, will release the current signal +1, and then wake up the waiting thread

Role: Multiple shared resources are mutually exclusive! Concurrent current limit, control the maximum number of threads!

 

 

 

Guess you like

Origin blog.csdn.net/weixin_45480785/article/details/105360417