1

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.

user3164187
  • 1,382
  • 3
  • 19
  • 50

2 Answers2

1

When you initialize CountDownLatch with some value for example:

CountDownLatch latch = new CountDownLatch(3);

It basically means that when method:

latch.countDown();

will be fired three times, the class which will use latch will be released from the await method call.

Of course you must ensure that the same instance of latch is used. For more information go to nice tutorial: http://tutorials.jenkov.com/java-util-concurrent/countdownlatch.html

wsl
  • 8,875
  • 1
  • 20
  • 24
1

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

}
alex2410
  • 10,904
  • 3
  • 25
  • 41
  • From the example given by you, countDown() is done is thread t1. Say for example i am calling countDown() in EDT and same instance of countDown latch's countDown() in a seperate thread as well. is it correct to call countDown() in EDT? is it possible that countDown() in EDT might cause deadLock? Please see my edit.. – user3164187 May 19 '15 at 05:33
  • @user3164187 you can call `latch.countDown();`from any thread( also `EDT`). Seems deadlock can be according to that [bug report](https://bugs.openjdk.java.net/browse/JDK-6982348) but it fixed in version 7. Also not reccomend to call `latch.await();` in `EDT` because of blocking UI thread. – alex2410 May 19 '15 at 06:28