自己实现CountDownLatch

 自己实现的CountDownLatch ,只是模拟他的功能而已。jdk中的实现采用的是AQS

public class MyCountDownLatch {
    
    private final int total;

    private int counter = 0;

    public MyCountDownLatch(int total) {
        this.total = total;
    }

    public void countDown() {
        synchronized (this) {
            this.counter++;
            this.notifyAll();
        }
    }

    public void await() throws InterruptedException {
        synchronized (this) {
            while (counter != total) {
                this.wait();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/moris5013/p/10905771.html