javamultithreadingconcurrencydemocountdownlatch

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


In this program why All countdownlatch over message printed in between .Although it should wait for all the countdown latch to get over .As an extra Thread is started in the main method but that should be handled as cdl.countDown() method is called to handle countdown for this thread . Why its violating countdown latch ?

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());
        }
    }

}

Solution

  • Your print statement in CDLTask is after cdl.countDown() that why you get the message all over in between. The thread may be preempted between the count down and print statements then your main may be signaled before all the prints are done.