0
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Processor implements Runnable {
    private CountDownLatch latch;

    public Processor(CountDownLatch latch) {
        this.latch = latch;
    }

    public void run() {
        System.out.println("Started.");

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        latch.countDown();
    }
}
// -----------------------------------------------------

public class App {

    public static void main(String[] args) {

        CountDownLatch latch = new CountDownLatch(5); // coundown from 5 to 0

        ExecutorService executor = Executors.newFixedThreadPool(2); // 2 Threads in pool

        for(int i=0; i < 10; i++) {
            executor.submit(new Processor(latch)); // ref to latch. each time call new Processes latch will count down by 1
        }

        try {
            latch.await();  // wait until latch counted down to 0
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Completed.");
    }

}

Output:

Started Started Started Started Started Started Started Completed` Started Started Started

In the above code "Completed" should be printed after 6 times "Started" as latch count downs to 5 - 0, why it is always 7th or 8th time? Have i understood wrong?

iramshiv
  • 3
  • 3
  • `new CountDownLatch(5); // coundown from 3 to 0` do you mean 5, or 3? – David Conrad May 31 '18 at 15:41
  • 1
    `main` starts ten of them, and they all print Started as soon as they start. There's no reason you shouldn't happen to see all ten before `main` ever gets to awaiting the latch, other than sheer luck. – David Conrad May 31 '18 at 15:44

2 Answers2

1

Your thread pool has size 2 and your Processor threads take 3 seconds to execute.

  • First two Processors threads are started, the both print Started and they finish 3 seconds later.
  • Then the next two are started and again they both print Started and finish 3 second later.
  • Then another two (5th and 6th) are started, print Started and 3 seconds later when one of them (or both) finishes. At this point there are multiple things that are going to happen at roughly the same time (so the order is kind of random):

    1. The main thread resumes and prints Completed
    2. 7th Processor thread is started and prints Started
    3. 8th Processor thread is started and prints Started

Therefore, Completed is always going to be preceded by 6, 7 or 8 Started print outs.

Jaroslaw Pawlak
  • 5,538
  • 7
  • 30
  • 57
0

So, CountDownLatch does not guarantee that it will resume the parent thread (here I mean the thread from which you have called latch.await();) execution as soon as the Count Down goes to 0. So, what happens when Count Down latch countdown to 0 that means now parent Thread can resume its work and that does not mean it will get the get the CPU then and there. So, it can resume that does not mean CPU schedule the parent thread as soon as the countdown to 0. If there are other threads then is there is a possibility that those can execute before the parent thread. In your case, it ensures that it will not execute before the 5 time printing Started but it does ensure that it will be exactly after printing 5 times Started. You may also observe at a certain execution of your code Completed is printing at end of all Started printing.

Amit Bera
  • 7,075
  • 1
  • 19
  • 42