Java Reference
In-Depth Information
code increments a thread's priority to one unit higher than the normal priority:
...
Thread threadX = new Thread (this);
threadX.setPriority (Thread.NORM - PRIORITY + 1);
threadX.start ();
...
Attempting
to
set
a
thread's
priority
below MIN - PRIORITY or
above
MAX - PRIORITY results in an IllegalArgumentException .
For multiple threads in a non-preemptive system, once one of them starts
running it will continue until one of the following happens:
sleeps via an invocation of sleep()
yields control with yield()
waits for a lock in a synchronized method (synchronization is discussed in the next
section)
blocks on I/O such as a read() method waiting for data to appear
terminates with a return from run()
We will discuss synchronization and the wait() method in the following section.
8.5 Using multiple threads
Programs for some tasks become much easier to design with threads, sometimes
with lots of threads. We've already mentioned animations, and in Part II we will
see that client/server systems lend themselves naturally to multithreaded design -
the server can spin off a new thread to service each client. Some mathematical
algorithms, such as sorting and prime searching, also work well with multiple
threads working on different segments of the problem. On multiprocessor sys-
tems, JVMs can take advantage of true parallel processing and provide significant
speedups in performance for multithreaded applications.
There are basically four situations in which multiple threads operate:
1. Non-interacting threads - the actions of the threads are completely independent and
do not affect each other.
2. Task splitting - each thread works on a separate part of the same problem, such as
sorting different sections of a data set, but do not overlap with each other.
3. Exclusive thread operations - the threads work on the same data and must avoid
interfering with each other. This requires synchronization techniques.
4. Communicating threads - the threads must pass data to each other and do it in the
correct order.
The latter two cases can entail complex and often subtle interference problems
among the threads. We look in more detail at these four cases in the following
sections.
Search WWH ::




Custom Search