Search code examples
javamultithreadingcountdownlatch

Usage of countDown latch in java


Am new to java programming and first time using countDown in java,

My code snippet is,

CountDownLatch latch=new CountDownLatch(rows*columns); //rows -2 , columns -3
        for (int i = 0; i < rows; i++) {
                    for (int j = 0; j < columns; j++) {
                            GUIView view = getView(1, 1);
                            if(view == null) {
                                if(ViewColumn(j +1) != null){
                                    latch.countDown(); //EDT
                                    continue;
                                }
                                 latch.countDown(); //EDT
                                 break;
                            }   
new Thread(new countDownThread(view,latch)).start(); //Where i do some other processing and do countDown 


                    }
        }
        try {
             logger.log("Before countdown await");
            latch.await();
            logger.log("After countdown await");

        }
        .........
        ........

As i read from another post,

One of the disadvantages/advantages of CountDownLatch is that its not reusable once count reaches to zero you can not use CountDownLatch any more.

My doubt here is am using the same instance latch , inside the for loop. if CountDownLatch is not reusable what will happen if the first iteration latch.countDown() starts and it became zero by third iteration(The latch.countDown() at third iteration is not valid??).

The problem is :

When i debug the for loop(using eclipse), and when control reaches latch.await(); it just hangs. However, if i just run the application no hang happens.

I don't quite understand usage of countDown latch. Please explain me on the same.


Solution

  • Seems here you don't use multithreading, and all work done in one thread, because of you needn't to use CountDownLatch.

    Also latch.await(); hang because it waiting for all count of tasks will be done(seems here it //rows -2 , columns -3 = 6) and call latch.countDown();. Read more about in docs.

    Here is simple example of use, where t2 wait for t1:

    import java.util.concurrent.CountDownLatch;
    
    public class Test {
    
        public static void main(String... s){
            final CountDownLatch cdl = new CountDownLatch(1);
    
            Thread t1 = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    int i = 5;
                    while(i-- > 0)
                        System.out.println("t2 wait me");
                    cdl.countDown();
                }
            });
    
    
            Thread t2 = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        cdl.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("done");
                }
            });
    
            t2.start();
            t1.start();
        }
    
    }