Why Use Multiple Threads?

In many situations, having more than one threadDo Other Things While Waiting for Slow I/O
running in a program is beneficial. Here's a moreOperations
in-depth look at why this can be good.Input and Output (I/O) operations to and from a
Better Interaction with the Userhard disk or especially across a network are relatively
If only one thread was available, a program would beslow when compared to the speed of code
able to do only one thing at a time. In the wordexecution in the processor. As a result, read/write
processor example, how nice it was to be able tooperations may block for quite a while, waiting to
open a second document while the first documentcomplete.
was being formatted and queued to the printer. InIn the java.io package, the class InputStream has a
some older word processors, when the user printedmethod, read(), that blocks until a byte is read from
a document, he or she had to wait while thethe stream or until an IOException is thrown. The
document was prepared for printing and sent to thethread that executes this method cannot do anything
printer. More modern word processors exploit multipleelse while awaiting the arrival of another byte on the
threads to do these two things at the same time. Instream. If multiple threads have been created, the
a one-processor system, this is actually simulated byother threads can perform other activities while the
the operating system rapidly switching back and forthone thread is blocked, waiting for input.
between two tasks, allowing for better userBy dividing the work between two threads, the GUI
interaction.event-handling thread is free to handle other
From the perspective of a microprocessor, even theuser-generated events. In particular, you might want
fastest typist takes a tremendous amount of timeanother button, labeled Cancel Request, that would
between keystrokes. In these large gaps of time,signal the worker thread to cancel the interaction
the processor can be utilized for other tasks. If onewith the server. If you had not used a worker
thread is always waiting to give a quick response tothread to perform the interaction with the server,
a user's actions, such as clicking the mouse orbut simply had the GUI event thread do the work,
pressing a key, while other threads are off doingthe interruption activity triggered by the Cancel
other work, the user will perceive better responseRequest button would not be possible.
from the system.Simplify Object Modeling
Simulation of Simultaneous ActivitiesObject-oriented analysis of a system before it is built
Threads in Java appear to run concurrently, evencan lead to a design requiring some of the objects to
when only one physical processor exists. Thehave a thread running within them. This kind of
processor runs each thread for a short time andobject can be thought of as active, as opposed to
switches among the threads to simulate simultaneouspassive. A passive object changes its internal state
execution. This makes it seem as if each thread hasonly when one of its methods is invoked. An active
its own processor, creating a virtual multipleobject may change its internal state autonomously.
processor system. By exploiting this feature, you canAs an example, consider building a digital clock
make it appear as if multiple tasks are occurringgraphical component that displays the current system
simultaneously when, in fact, each is running for onlytime in hours and minutes. Every 60 seconds, the
a brief time before the context is switched to theminutes (and possibly the hours) displayed on this
next thread.component will have to change. The simplest design
Exploitation of Multiple Processorsis to have a thread running inside the clock
In some machines, several real microprocessors arecomponent and dedicated to updating the digits
present. If the underlying operating system and thewhen necessary. Otherwise, an external thread would
implementation of the JavaVM exploit the use ofhave to continually check whether it is time to
more than one processor, multithreaded Javaupdate a digit, in addition to performing its other
programs can achieve true simultaneous threadduties. What if that external thread had to read data
execution. A Java program would not have to befrom an InputStream, and it was blocked, waiting for
modified because it already uses threads as if theya byte for longer than a minute? Here, exploiting the
were running on different processors simultaneously.benefits of multithreaded programming simplifies the
It would just be able to run even faster.solution.