Threads support concurrent operations. For example,
• Multiple requests by a client on a server can be handled as an individual client thread.
•
Long computations or high-latency disk and network operations can be
handled in the background without disturbing foreground computations or
screen updates.
Threads often result in simpler programs.
•
In sequential programming, updating multiple displays normally
requires a big while-loop that performs small parts of each display
update. Unfortunately, this loop basically simulates an operating
system scheduler. In Java, each view can be assigned a thread to
provide continuous updates.
• Programs that need to respond to
user-initiated events can set up service routines to handle the events
without having to insert code in the main routine to look for these
events.
Threads provide a high degree of control.
•
Imagine launching a complex computation that occasionally takes longer
than is satisfactory. A "watchdog" thread can be activated that will
"kill" the computation if it becomes costly, perhaps in favor of an
alternate, approximate solution. Note that sequential programs must
muddy the computation with termination code, whereas, a Java program
can use thread control to non-intrusively supervise any operation.
Threaded applications exploit parallelism.
•
A computer with multiple CPUs can literally execute multiple threads
on different functional units without having to simulating
multi-tasking ("time sharing").
• On some computers, one CPU handles
the display while another handles computations or database accesses,
thus, providing extremely fast user interface response times.