Java Reference
In-Depth Information
can appear to do their work in parallel with each other. This is the basic understanding
required for the SCJP exam, and it should be material you have mastered if you are thinking
about taking the SCJD exam.
A Brief Review of Threads
A thread is an independent stream of execution for a set of instructions. For example, imagine
a word processor needs to accomplish two activities, as shown in Figure 4-1. It needs to print
and save the document you are currently working on.
Figure 4-1. Word processing program example
If the word processor were single-threaded, it could not start saving the document until it
was finished printing the document, as depicted in Figure 4-1. This approach would certainly
work, but it would be overly inefficient, as those of us who remember the earliest word proces-
sors can attest. We can do better.
Now imagine that the program spawns two independent paths of execution, or threads.
One is printing a document, and the other is quietly backing up the document in the back-
ground. This approach will yield potentially more efficient results, because there is no need
for the two activities to wait on each other. For example, even as the network connection is
being established with the printer, another thread could be saving the file. This concept of
multithreading is illustrated in Figure 4-2 and described in detail in the next section.
Figure 4-2. Word processing program with threads
Search WWH ::




Custom Search