javamultithreadingthread-priority

Java - is thread priority absolute?


For example, I run several endless threads with different priority. When thread swiching is taking place will thread with low priority ever be selected? Is priority absolute or it just affect on the thread selection probability.


Solution

  • Most schedulers are based on multilevel feedback queues. These priorities are based mostly on the program's behavior. The kernel takes your assigned thread priority as more of a 'hint'.

    For example, if a thread does a lot of IO, then it could be scheduled more often even if it has a lower priority than another thread because of how the MFQ operates. Threads that spend more of their schedule quantum will be moved to lower priorities in the MFQ. Threads that give up some of their quantum (i.e. for IO) will likely stay at their current priority.

    If you assign thread 1 a higher priority than thread 2, and they are working on similar tasks, there is a higher probability that thread 1 will get scheduled. This doesn't mean that thread 1 must be scheduled more often than thread 2, it just hints to the kernel that you would like it to be scheduled more often than thread 2. In the end it is up to the kernel which thread gets scheduled when.