Thread Scheduling and Priority
Home ] Up ] What is MultiThreading? ] Threads & Thread Properties ] [ Thread Scheduling and Priority ] Types of Threads ] Monitors, Synchronization and Deadlocks ] Thread Coordination ] Communication Between Threads ] Threads and Thread Groups ] Explicit Locks ] Atomic Variables ] Blocking Queues ] Thread-Safe Collections ] Callables & Futures ] The Executor Framework ] Synchronizers ]

 

 

Thread Scheduling

The mechanism used to allocate CPU time to threads.

There are two basic types of thread scheduling:

  • Preemptive
  • Non-preemptive

Preemptive Scheduling

In this type of scheduling, the scheduler preempts a running thread to allow other threads to run. The process of switching from one thread running to another thread running is transparent to the writer of the thread code.

Non-Preemptive Scheduling

Here, the scheduler never interrupts a running thread. Instead, the thread code is expected to cooperate and yield control at the appropriate times so that other threads can execute. If a thread does not cooperate, it may cause other threads to starve (never get CPU time). for example, if a thread has code like:

while (forALongTime)
{
    codeThatDoesNotYieldNorBlock();
}

then, with a non-preemptive scheduler, other threads would not get scheduled while this thread was running this piece of code.

Here are some things to do when you are writing code that must run in a thread with a non-preemptive scheduler:

  • Don't loop for a long time executing code that does not yield, nor sleeps, nor does blocking I/O.
  • Occasionally call yield() when performing CPU-intensive operations.
  • Lower the priority of CPU-intensive threads (but they still need to yield).

Writing for preemptive thread models is easier than writing for non-preemptive thread models. Note that, if you are trying to write portable Java code, and you're developing on a platform that uses preemptive threads, you can't make assumptions that all Java platforms will provide this level of thread support.

Thread Priority

Different native operating system threads have different thread priority schemes. The Java Thread class attempts to accommodate these in its abstraction.

In the Thread class, there are the following constants:

public static final int MAX_PRIORITY
The maximum priority that a thread can have.
public static final int MIN_PRIORITY
The minimum priority that a thread can have.
public static final int NORM_PRIORITY
The default priority assigned to a thread.

which are mapped to the native thread priorities.

There are also the following methods in class Thread:

public final int getPriority()
Returns this thread's priority.
public final void setPriority(
                    int newPriority)
Changes the priority of this thread.
(The priority must be in the range MIN_PRIORITY to MAX_PRIORITY, and the calling thread must have the proper access rights to change the thread priority.)

The setPriority() method may be called to change the priority of a thread. Typically, the call would look like:

thread.setPriority(NORM_PRIORITY + 1);

or:

thread.setPriority(NORM_PRIORITY - 3);

Be aware:

  • It should only be necessary to make small changes to a thread's priority.
  • If you elevate too many threads' priorities to MAX_PRIORITY (or close to it), priority will quickly become meaningless.

 

The page was last updated February 19, 2008