Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread becomes disabled for thread scheduling purposes. What does that mean?

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?

like image 419
prasanth Avatar asked Jan 09 '23 20:01

prasanth


1 Answers

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!

like image 151
templatetypedef Avatar answered Jan 15 '23 08:01

templatetypedef