Java Reference
In-Depth Information
Chapter 22. Threaded Java
Introduction
We live in a world of multiple activities. A person may be talking on the phone while dood-
ling or reading a memo. A multifunction office machine may scan one fax while receiving
another and printing a document from somebody's computer. We expect the GUI programs
we use to be able to respond to a menu while updating the screen. But ordinary computer
programs can do only one thing at a time. The conventional computer programming mod-
el—that of writing one statement after another, punctuated by repetitive loops and binary de-
cision making—is sequential at heart.
Sequential processing is straightforward but not as efficient as it could be. To enhance per-
formance, Java offers threading, the capability to handle multiple flows of control within a
single application or process. Java provides thread support and, in fact, requires threads: the
Java runtime itself is inherently multithreaded. For example, window system action handling
and Java's garbage collection—that miracle that lets us avoid having to free everything we
allocate, as others must do when working in languages at or below C level—run in separate
threads.
Just as multitasking allows a single operating system to give the appearance of running more
than one program at the same time on a single-processor computer, so multithreading can al-
low a single program or process to give the appearance of working on more than one thing at
the same time. With multithreading, applications can handle more than one activity at the
same time, leading to more interactive graphics and more responsive GUI applications (the
program can draw in a window while responding to a menu, with both activities occurring
more or less independently), more reliable network servers (if one client does something
wrong, the server continues communicating with the others), and so on.
Note that I did not say “multiprocessing” in the previous paragraph. The term multi-tasking
is sometimes erroneously called multiprocessing, but that term in fact refers to different is-
sue: it's the case of two or more CPUs running under a single operating system. Multipro-
cessing per se is nothing new: IBM mainframes did it in the 1970s, Sun SPARCstations did it
in the late 1980s, and Intel PCs did it in the 1990s. Since the mid-2010s, it has become in-
creasingly hard to buy a single-processor computer packaged inside anything larger than a
Search WWH ::




Custom Search