| In many situations, having more than one thread | | | | Do Other Things While Waiting for Slow I/O |
| running in a program is beneficial. Here's a more | | | | Operations |
| in-depth look at why this can be good. | | | | Input and Output (I/O) operations to and from a |
| Better Interaction with the User | | | | hard disk or especially across a network are relatively |
| If only one thread was available, a program would be | | | | slow when compared to the speed of code |
| able to do only one thing at a time. In the word | | | | execution in the processor. As a result, read/write |
| processor example, how nice it was to be able to | | | | operations may block for quite a while, waiting to |
| open a second document while the first document | | | | complete. |
| was being formatted and queued to the printer. In | | | | In the java.io package, the class InputStream has a |
| some older word processors, when the user printed | | | | method, read(), that blocks until a byte is read from |
| a document, he or she had to wait while the | | | | the stream or until an IOException is thrown. The |
| document was prepared for printing and sent to the | | | | thread that executes this method cannot do anything |
| printer. More modern word processors exploit multiple | | | | else while awaiting the arrival of another byte on the |
| threads to do these two things at the same time. In | | | | stream. If multiple threads have been created, the |
| a one-processor system, this is actually simulated by | | | | other threads can perform other activities while the |
| the operating system rapidly switching back and forth | | | | one thread is blocked, waiting for input. |
| between two tasks, allowing for better user | | | | By dividing the work between two threads, the GUI |
| interaction. | | | | event-handling thread is free to handle other |
| From the perspective of a microprocessor, even the | | | | user-generated events. In particular, you might want |
| fastest typist takes a tremendous amount of time | | | | another 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 one | | | | with the server. If you had not used a worker |
| thread is always waiting to give a quick response to | | | | thread to perform the interaction with the server, |
| a user's actions, such as clicking the mouse or | | | | but simply had the GUI event thread do the work, |
| pressing a key, while other threads are off doing | | | | the interruption activity triggered by the Cancel |
| other work, the user will perceive better response | | | | Request button would not be possible. |
| from the system. | | | | Simplify Object Modeling |
| Simulation of Simultaneous Activities | | | | Object-oriented analysis of a system before it is built |
| Threads in Java appear to run concurrently, even | | | | can lead to a design requiring some of the objects to |
| when only one physical processor exists. The | | | | have a thread running within them. This kind of |
| processor runs each thread for a short time and | | | | object can be thought of as active, as opposed to |
| switches among the threads to simulate simultaneous | | | | passive. A passive object changes its internal state |
| execution. This makes it seem as if each thread has | | | | only when one of its methods is invoked. An active |
| its own processor, creating a virtual multiple | | | | object may change its internal state autonomously. |
| processor system. By exploiting this feature, you can | | | | As an example, consider building a digital clock |
| make it appear as if multiple tasks are occurring | | | | graphical component that displays the current system |
| simultaneously when, in fact, each is running for only | | | | time in hours and minutes. Every 60 seconds, the |
| a brief time before the context is switched to the | | | | minutes (and possibly the hours) displayed on this |
| next thread. | | | | component will have to change. The simplest design |
| Exploitation of Multiple Processors | | | | is to have a thread running inside the clock |
| In some machines, several real microprocessors are | | | | component and dedicated to updating the digits |
| present. If the underlying operating system and the | | | | when necessary. Otherwise, an external thread would |
| implementation of the JavaVM exploit the use of | | | | have to continually check whether it is time to |
| more than one processor, multithreaded Java | | | | update a digit, in addition to performing its other |
| programs can achieve true simultaneous thread | | | | duties. What if that external thread had to read data |
| execution. A Java program would not have to be | | | | from an InputStream, and it was blocked, waiting for |
| modified because it already uses threads as if they | | | | a 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. |