Java Multithreading, Concurrency, and Parallelism — Part 6

KRISHNA KISHORE V
6 min readAug 5, 2021

Thread Priorities and yield()

In part-5 we have seen how the order of thread execution happens and the join() method to join the currently running thread at the end of the other thread. We have also learned that there is no guarantee in the order of threads' execution. Here in this part, we’ll have a look at the thread priorities and how we expect to gracefully threads can take their turns using the yield(). But before understanding the yield() method, it is really important to look at thread priorities.

In Java, thread priorities are just the numbers from 1 to 10 (1: being the lowest priority and 10: the highest). The scheduler in most JVMs is priority-based preemptive scheduling with some sort of time slicing. What does that mean? Preemptive essentially means turn-taking. The threads take CPU’s turn after some time elapses and which thread to take its turn next depends on the priority of the thread.

But the thread scheduling algorithm varies from JVM to JVM. And even JVM specification doesn't specify which scheduling algorithm to use. It all depends on how the thread scheduler is implemented in a particular JVM. We cannot even guarantee whether all the JVMs use thread priorities in the first place. They may also use Round-Robin with a time slice. There is no such rule enforced anywhere in JVM…

--

--

KRISHNA KISHORE V

Full Stack Tech Lead | Software Consultant | Technical Content Writer