Java Reference
In-Depth Information
Thread Priorities
All threads have a priority that determines which thread is executed when several threads are waiting
for their turn. This makes it possible to give one thread more access to processor resources than
another. Let's consider an elementary example of how this could be used. Suppose you have one thread
in a program that requires all the processor resources - some solid long running calculation- and some
other threads that require relatively few resources. By making the thread that requires all the resources a
low priority thread, you ensure that the other threads get executed promptly, while the processor bound
thread can make use of the processor cycles that are left over after the others have had their turn.
The possible values for thread priority are defined in
static
data members of the class
Thread
. These
members are of type
int
, and are declared as
final
. The maximum thread priority is defined by the
member
MAX
_
PRIORITY
, which has the value 10. The minimum priority is
MIN
_
PRIORITY
, defined as 1.
The value of the default priority that is assigned to the main thread in a program is
NORM
_
PRIORITY
, which
is set to 5. When you create a thread, its priority will be the same as that of the thread that created it.
You can modify the priority of a thread by calling the
setPriority()
method for the
Thread
object.
This method accepts an argument of type
int
that defines the new priority for the thread. An
IllegalArgumentException
will be thrown if you specify a priority that is less than
MIN
_
PRIORITY
or greater than
MAX
_
PRIORITY
.
If you're going to be messing about with the priorities of the threads in your program, you need to be able to
find out the current priority for a thread. You can do this by calling the
getPriority()
method for the
Thread
object. This will return the current priority for the thread as a value of type
int
.
Using Thread Priorities
In the last example, you could set priorities for the threads by adding statements to
main()
:
clerk1Thread.setPriority(Thread.MIN
_
PRIORITY); // Credits are a low priority
clerk2Thread.setPriority(Thread.MAX
_
PRIORITY); // Debits are a high priority
You can put these statements following the call to the
start()
method for each of the
Thread
objects for
the clerks. However, this can't have much effect in our program since one clerk can't get ahead of the other.
This is because each clerk only queues one transaction and they are allocated alternately to each clerk.
In the interests of learning more about how thread priorities affect the execution of your program, let's
change the example once more to enable a
Clerk
object to queue transactions. We can do this quite
easily using a
LinkedList
object, which we discussed in Chapter 13. There are a couple of points to
be aware of though.
The first point is that only the
Vector
class out of the collection classes is thread-safe - that is, safe for
modification by more than one thread. For the others you must either only access them by methods and
code blocks that are synchronized on the collection object, or wrap the collection class in a thread-safe
wrapper. Let's change the example to incorporate the latter.
The second point is that whether thread priorities have any effect depends on your operating system. If
it doesn't support thread priorities, then setting thread priorities in your Java code will have no effect.
Let's run it anyway to see how it works.