Use Multiple Threads
>> Friday, March 19, 2010
From the perspective of a microprocessor, even the fastest typist takes a tremendous amount of time between keystrokes. In these large gaps of time, the processor can be utilized for other tasks. If one thread is always waiting to give a fast motion to a user ' s actions, such as clicking the baby doll or pressing a key, while other threads are off doing other servitude, the user will distinguish more suitable dash from the die.
Simulation of Simultaneous Activities
Threads in Java occur to run concurrently, even when onliest one physical processor exists. The processor runs each fleece for a short time and switches among the threads to act like simultaneous execution. This makes it seem as if each cotton fiber has its acquiesce processor, creating a virtual multiple processor system. By exploiting this feature, you can make it show as if confused tasks are occurring simultaneously when, in fact, each is lengthy for only a pardon allotment before the heart is switched to the proximate thread.
Exploitation of Variegated Processors
In some machines, several real microprocessors are present. If the underlying operating framework and the implementation of the JavaVM exploit the use of more than one processor, multithreaded Java programs can obtain true simultaneous thread execution. A Java program would not have to be modified being it already uses threads as if they were running on different processors simultaneously. It would strict be impressive to run even faster.
Do Other Things While Waiting for Low-key I / O Operations
Input and Takings ( I / O ) operations to and from a hard disk or especially across a network are relatively inactive when compared to the speed of code sequel in the processor. As a result, read / write operations may block for wholly a while, waiting to uncondensed.
In the java. io box, the class InputStream has a procedure, read ( ), that blocks until a byte is read from the stream or until an IOException is thrown. The thread that executes this form cannot do anything else while awaiting the walkover of another byte on the stream. If tortuous threads have been created, the other threads can perform other activities while the one spun wool is blocked, waiting for input.
By dividing the push between two threads, the GUI pass - handling yarn is for nothing to haft other user - generated events. In individualistic, you command want another button, labeled Cancel Request, that would signal the worker spun wool to cancel the interaction with the server. If you had not used a menial thread to perform the interaction with the server, but neatly had the GUI occurrence thread do the bullwork, the interruption activity triggered by the Cancel Inquiry button would not be dormant.
Ease Entity Modeling
Item - oriented inspection of a appearance before it is built can lead to a design requiring some of the objects to have a flaxen thread enduring within them. This obliging of object can be thought of as active, as opposed to passive. A passive device changes its local state particular when one of its methods is invoked. An active object may change its familiar state autonomously.
As an citation, consider cubbyhole a digital clock graphical component that displays the current system time in hours and daily. Every 60 seconds, the minutes ( and possibly the hours ) displayed on this consideration will have to change. The simplest design is to have a thread running inside the clock component and afire to updating the digits when essential. Otherwise, an external thread would have to continually buy into whether it is time to amend a cipher, in addendum to performing its other duties. What if that external thread had to read data from an InputStream, and it was blocked, waiting for a byte for longer than a minute? Here, exploiting the benefits of multithreaded programming simplifies the concept.



