Thread Priorities
Java assigns to each thread a priority that determines how that thread should be treated with respect to the others. Thread priorities are integers that specify the relative priority of one thread to another. As an absolute value, a-priority is meaningless higher-priority thread doesn't flm-any faster than a lower-priority thread Wit is the only thread running. Instead, a thread's priority is used to decide when to switch from ne running thread to the next. This is called a context switch. The rules that determine when a context switch takes place are simple: .
• A thread can voluntarily relinquish 'control. done byexplicitly yielding, sleeping! ~r blocking 01'1 pending I/O. In this scenario, all other threads are examined, and the highest-priority thread that is ready to run is given the CPU.

• A thread enn be preempted by a higher-priority thread. In this case, a lower-priority thread that does not yield the processor is simply preempted-one matter what it is doing-by a higher-priority thread. Basically, as soon as a higher-priority thread wants to rim, it does. This is called preemptive multitasking.

In cases where two threads with the same priority arc competing for CPU cycles the situation is a bit complicated. For operating systems such as Windows 98, threads of equal priority are time-sliced automatically in round-robin fashion. For other types of operating systems, such as Solaris 2.x, threads of equal priority must voluntarily' yield control to their peers. If they don't, the other threads will not run.

Share This