1

Suppose i have a countdown latch of size 3 i.e. 3 threads are spawned off from parent thread.

CountDownLatch latch = new CountDownLatch(3);

latch.await().

Now there will be three threads which will be calling countDown after their respective task completion.

// do something
 latch.countDown();

My question is that the moment task gets completed whats the behavior of thread that was executing that task.

Does that thread gets terminated right at that moment, or waits for sometime by going into idle state. ?

I could find it anywhere on java doc of countdown latch.

Timothy Drisdelle
  • 395
  • 3
  • 5
  • 14
  • 1
    I don't think it affects thread lifecycle, aside from possibly causing a thread yield (to allow the latched thread to resume), but in terms of thread lifecycle, I don't think it terminates it entirely. – Shotgun Ninja Jun 15 '15 at 14:17
  • I've done no research on it, however. – Shotgun Ninja Jun 15 '15 at 14:17
  • The thread will enter the `WAITING` state and will "sleep" until it is reawakened by the scheduler. If, at that point, it's `Runnable.run` method returns, the `Thread` will die. There is no way that the `Thread` could know, before `Runnable.run` exits, whether it is required to execute more code or not. So the behaviour is the same as calling `Object.wait`. – Boris the Spider Jun 15 '15 at 14:20
  • The reason you can't find it in the java doc is that it has no effect on thread termination. However if you want the 3 threads to run the same task repeatedly, but only when the thread doing the `await()` isn't doing its processing, you'll need more signalling. – biziclop Jun 15 '15 at 14:24
  • This question needs a bigger source code example: Show us some methods, and then ask a question like "When threads A, B, and C call method1() and thread D calls method2(), what happens to thread D when..." – Solomon Slow Jun 15 '15 at 16:07

2 Answers2

1

It depends upon the code in the child threads. If there is more computation to do after the CountDownLatch it will then continue with the computation. If not, the the thread will termninate.

Brett Walker
  • 3,566
  • 1
  • 18
  • 36
  • 1
    Also note that this is entirely independent of the presence or absence of the countdown latch. The latch doesn't directly affect the termination of any threads in any way. – biziclop Jun 15 '15 at 14:21
1

You are focusing too much on the typical example of having N subordinate threads signal to a master thread.

This CountDownLatch thing is just a class that allows code to block until some external agents count down the latch to 0. Period.

These agents can be N different threads as in your example, or it can be a single thread counting down multiple times. Therefore you should stop viewing those agents as threads. The latch doesn't know them and doesn't care if instead of completing they go have a beer at the pub after counting down. It is not a Thread Manager and it doesn't monitor the lifecycle of any thread.

Mister Smith
  • 27,417
  • 21
  • 110
  • 193