Threads are assigned priorities that the thread scheduler can use to determine how the threads will be scheduled. The thread scheduler can use thread priorities to determine which thread gets to run. The thread scheduler favors giving CPU time to the thread with the highest priority in the Ready-to-run state. This is not necessarily the thread that has been the longest time in the Ready-to-run state. Heavy reliance on thread priorities for the behavior of a program can make the program unportable across platforms, as thread scheduling is host platform-dependent.
Priorities are integer values from 1 (lowest priority given by the constant
Thread.MIN_PRIORITY) to 10 (highest priority given by the constant
The default priority is 5 (
A thread inherits the priority of its parent thread. Priority of a thread can be set using the
setPriority() method and read using the
getPriority() method, both of which are defined in the Thread class.
The following code sets the priority of the thread myThread to the minimum of two values: maximum priority and current priority incremented to the next level: