When I was going through Javadoc for CountDownLatch, I came across a line in the documentation for await method.
If the current count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant
What is meant by current thread becomes disabled for thread scheduling purposes
here?
On a given system, only a fixed number of threads can actually execute at the same time (you're limited by the number of cores in the machine.) When there are more threads to run than there are cores, the thread scheduler will cycle through the threads in some fashion, giving each a bit of time on the CPU.
However, in some cases it doesn't make sense to give a thread time on the CPU. For example, if a thread acquires a countdown latch whose total is greater than zero, then that thread is stuck waiting for other threads to also acquire the latch. It therefore doesn't make any sense to let that thread have any CPU time, since the thread is just sitting and waiting for other threads. Therefore, typically, the scheduler would not even attempt to give the thread any CPU time, preferring instead to schedule other threads that can still make progress. Once enough threads do acquire the countdown latch, all threads that were blocked this way are then put back into the scheduler for further consideration.
In other words, the thread stops running and the scheduler will intelligently not waste time trying to run it until the latch is ready.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With