【问题标题】:CountdownLatch Demo program .Not waiting for coutdown latch to get overCountdownLatch 演示程序。不等待 countdownlatch 结束
【发布时间】:2020-08-14 10:38:55
【问题描述】:

在这个程序中,为什么 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());
        }
    }

}

【问题讨论】:

    标签: java multithreading concurrency demo countdownlatch


    【解决方案1】:

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

    【讨论】:

    • 有没有办法控制它,使其不会被抢占并等待倒计时闩锁达到 0,然后只有我的主线程打印 System.out.println("All completed");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多