|
|
|
|
Thread SchedulingThe mechanism used to allocate CPU time to threads.There are two basic types of thread scheduling:
Preemptive SchedulingIn 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 SchedulingHere, 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:
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:
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 PriorityDifferent 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:
which are mapped to the native thread priorities. There are also the following methods in class Thread:
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:
|
| The page was last updated February 19, 2008 |