concurrent中的组件

发布时间 2023-07-26 19:48:32作者: Kavins

  CountDownLatch可以设定一个初始值,调用其countDown()方法会使得设定值减一,调用它的await()方法会使得调用它的线程处于阻塞状态,直至设定值变为0

/**
 * 一个程序可以分为多个相互独立的待解决的问题(TaskPortion),等待所有问题被解决后才能执行的任务(WaitingTask)
 * 会在CountDownLatch(锁存器)上调用await(),将自己拦住,直至所有问题被解决
 * @date 2022/3/14
 */
class TaskPortion implements Runnable {
    private static int counter = 0;
    private final int id = counter++;
    private static final Random RAND = new Random(47);
    private final CountDownLatch latch;

    public TaskPortion(CountDownLatch latch) {
        this.latch = latch;
    }
    @Override
    public void run() {
        try {
            doWork();
            latch.countDown();
        } catch (InterruptedException e) {

        }
    }

    public void doWork() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(RAND.nextInt(2000));
        System.out.println(this + " completed");
    }

    @Override
    public String toString() {
        return String.format("%1$-3d ", id);
    }
}

class WaitingTask implements Runnable {
    private static int counter = 0;
    private final int id = counter++;
    private final CountDownLatch latch;

    public WaitingTask(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            latch.await();
            System.out.println("Latch barrier passed for " + this);
        } catch (InterruptedException e) {
            System.out.println(this + " interrupted");
        }
    }

    @Override
    public String toString() {
        return String.format("WaitingTask %1$-3d ", id);
    }
}

public class CountDownLatchDemo {
    static final int SIZE = 100;

    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        CountDownLatch latch = new CountDownLatch(SIZE);
        for (int i = 0; i < 10; i++) {
            // WaitingTask会等到所有的TaskPortion任务完成后再执行,因为WaitingTask中执行了latch.await();
            exec.execute(new WaitingTask(latch));
        }
        for (int i = 0; i < SIZE; i++) {
            exec.execute(new TaskPortion(latch));
        }
        System.out.println("Latched all tasks");
        exec.shutdown();
    }
}