CountdownLatch 演示程序。不等待 countdownlatch 结束

CountdownLatch Demo program .Not waiting for coutdown latch to get over

在这个程序中,为什么 All countdownlatch over message 打印在两者之间。尽管它应该等待所有倒计时锁存器结束。因为在 main 方法中启动了一个额外的线程,但应该将其处理为 cdl.countDown() 方法被调用来处理此线程的倒计时。 为什么它违反了倒计时闩锁?

import java.util.concurrent.CountDownLatch;

public class CDLDemo implements Runnable {

    static int count = 0;

    CountDownLatch cdl = new CountDownLatch(5);

    void checkForAwait() {
        cdl.countDown();
        System.out.println("Start " + cdl.getCount());
        CDLTask1 cdlTask1 = new CDLTask1(cdl);
        CDLTask2 cdlTask2 = new CDLTask2(cdl);
        CDLTask3 cdlTask3 = new CDLTask3(cdl);
        CDLTask4 cdlTask4 = new CDLTask4(cdl);

        Thread t1 = new Thread(cdlTask1);
        Thread t2 = new Thread(cdlTask2);
        Thread t3 = new Thread(cdlTask3);
        Thread t4 = new Thread(cdlTask4);

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        try {
            cdl.await();
            System.out.println("All countdownlatch over");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void run() {
        checkForAwait();
    }

    public static void main(String[] args) throws InterruptedException {
        CDLDemo cdlDemo = new CDLDemo();
        Thread t1 = new Thread(cdlDemo, "Thread1");
        t1.start();
        t1.join();
        System.out.println("All completed");
    }

    static int getCount() {
        return count++;
    }


    static class CDLTask1 implements Runnable {
        CountDownLatch cdl;

        public CDLTask1(CountDownLatch cdl) {
            this.cdl = cdl;
        }

        @Override
        public void run() {
            getCount();
            cdl.countDown();
            System.out.println("Available count :" + cdl.getCount() + "" + Thread.currentThread().getName());
        }
    }

    static class CDLTask2 implements Runnable {
        CountDownLatch cdl;

        public CDLTask2(CountDownLatch cdl) {
            this.cdl = cdl;
        }

        @Override
        public void run() {
            cdl.countDown();
            System.out.println("Available count :" + cdl.getCount() + "" + Thread.currentThread().getName());
        }
    }

    static class CDLTask3 implements Runnable {
        CountDownLatch cdl;

        public CDLTask3(CountDownLatch cdl) {
            this.cdl = cdl;
        }

        @Override
       public void run() {
            cdl.countDown();
            System.out.println("Available count :" + cdl.getCount() + "" + Thread.currentThread().getName());
        }
    }

    static class CDLTask4 implements Runnable {
        CountDownLatch cdl;

        public CDLTask4(CountDownLatch cdl) {
            this.cdl = cdl;
        }

        @Override
        public void run() {
            cdl.countDown();
            System.out.println("Available count :" + cdl.getCount() + "" + Thread.currentThread().getName());
        }
    }

}

您在 CDLTask 中的打印语句在 cdl.countDown() 之后,这就是为什么您在两者之间收到消息的原因。该线程可能会在倒计时和打印语句之间被抢占,然后您的主线程可能会在所有打印完成之前发出信号。