Chapter 24. Threaded Java

Introduction

We live in a world of multiple activities. A person may be talking on the phone while doodling or reading a memo. A FAX machine may scan one FAX while receiving another and printing a third. 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 model -- that of writing one statement after another, punctuated by repetitive loops and binary decision-making -- is sequential at heart.

Sequential processing is straightforward, but not as efficient as it could be. To enhance performance, 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, Windows system action handling and Java’s garbage collection -- that miracle that lets us avoid having to free everything we allocate, as we must do when working in languages at or below C level -- run in separate threads.

While most modern operating systems -- POSIX P1003, Sun Solaris, the Distributed Computing Environment (OSF/DCE) for Unix, Windows NT/95, and MacOS -- provide threads, Java is the first mainstream programming language to have intrinsic support for threaded operations built right into the language. The semantics of java.lang.Object, of which all objects are instances, includes the notion of " monitor locking” of objects, and some methods (notify, notifyall, wait) that are only meaningful in the context of a multithreaded application. Java also has language keywords such as synchronized to control the behavior of threaded applications.

So what, precisely, do we mean by threaded Java? Threads can be defined in several ways, but the easiest to understand is this: threads are multiple flows of control within a single program or process. 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 allow 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 the less-common case of two or more CPUs running under a single operating system. Actually, multiprocessing 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. True multiprocessing[56] allows you to have more than one process running concurrently on more than one CPU. Java’s support for threading will support multiprocessing under certain circumstances, if the operating system and the JVM support it as well. Consult your system documentation for details.



[56] By which I mean SMP, symmetric multiprocessing , in which either the operating system or the application programs can be run on any of the available CPUs. At some point, the OS may be running on three of the four processors on a given system, while at some later time all four processors may be running user processes. On systems such as Solaris 2.x, it is even possible for one (threaded) process to be running on several CPUs concurrently. The implications for server responsiveness are exciting, and this is part of Sun’s commercial success in the Internet server marketplace.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.177.39