Java Reference
In-Depth Information
}
This creates and starts a thread for each
Clerk
object in the
clerks
vector. After executing this loop you
have a total of five threads running, including the main thread.
You retrieve the values returned by the
TransactionSource
threads by calling the
get()
method for the
Future<>
object that is returned when you call
submit()
for the
ExecutorService
object. This occurs
in a
try
block to deal with the exceptions that can be thrown.
Before producing the final output summarizing the status of each account, you shut down the threads
and the thread pool in an orderly fashion. The first step is to call
shutdown()
for the
threadPool
object
to ensure no further threads can start. You then call
awaitTermination()
to wait for the threads to ter-
minate. The method waits for up to 10 seconds for all the threads to end and continues execution if they
do not. If all the clerks have not completed their tasks after 10 seconds, you shut down the thread pool
anyway and output the results.
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 are 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 is 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
representing the thread. This method accepts an argument of type
int
that defines the new priority for the
thread. An
IllegalArgumentException
is 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 returns the current priority for the thread as a value of type
int
.
You need to keep in mind that the actual execution priority of a thread that you set by calling
setPri-
ority()
depends on the mapping between Java thread priorities and the native operating system priorities.
The thread scheduling algorithm that your operating system uses also affects how your Java threads execute
and what proportion of the processor time they are allocated.
Using Thread Priorities