Home           J2EE Concepts           JMS           Java Language           Contact           Back           Next           Bookmark this Page          










Some useful tips related to:
 - health & fitness
 - vehicle care
 - cooking
 - misc household tasks

Thread Priority


Java assigns priority to each thread to determine how a thread should be treated with respect to others. Thread Priorities are integers that determine relative priorities of one thread to another.

A higher priority thread does not mean that it would run faster; instead it means that this thread would be switched from a lower priority thread. This switching is called CONTEXT SWITCH.

Context switching is done based on following rules:
- A thread can relinquish control voluntarily : TBD by explicitly yielding, sleeping, pending I/O, blocking etc. In such case, all existing threads would be examined and highest priority thread would be given to CPU for execution.
- A thread can be preempted by a higher-priority thread: it would be preempted not matter what it is doing. This is called PREEMPTIVE MULTITASKING.


In case, two threads have same priority, the handling varies from OS to OS. Typically, Windows executes all same priority threads in round-robin manner whereas for all others, a thread must voluntarily yield control to its peers. If it doesn't do so, the other thread would not run.

Caution - This means, caution must be taken while dealing with context switching for equal priority threads for developing thread centric applications for varying platforms.