Use Multiple Threads

Posted by - May 22nd, 2010

In many situations, the program is more than one thread is running in profit. What good is this more in depth here is a quick look.

Better interaction with users

The program will use only one thread at a time would be able to do one thing if there was. In the first example of a word processor for formatting of documents and the printer is waiting for the how good it was to the second document. Some old editor, the user is ready to print the document sent to the printer, while he or she had to wait for the document to print. More modern word processors and multiple threads simultaneously using two things. In a single processor system, which is actually a virtual operating system to quickly switch back and forth between two tasks better and allow user interaction is intended.

Even the fastest typist in terms of a microprocessor, an enormous amount of time between the key will not be. In a large difference in time, the processor can be used for other tasks. While other things, other threads, one thread is always the same as clicking the mouse or pressing a button on the action from the user gives a fast response, have been waiting, the user is aware of a better response of the system .

Simulate concurrent activities

Java threads running at the same time, even if a single physical processor appear. Processors short term and switch between threads to simulate concurrent execution, each thread runs. It’s like a virtual multi-processor system, each thread creates its own processor is seen. This feature allows you to work at the same time a number of conditions before the next debate, when in fact, each of which is just a short time can make the symptoms.

Exploitation of multiple processors

In some machines, the presence of multiple microprocessors are real. JavaVM implementation of the underlying operating system and one or more processors for Java programs that can be achieved using simultaneous threads, use multi-threaded to run. Java programs simultaneously on different processors, as is already a thread because it uses will not be adjusted. I will be able to run faster.

I / O operations while waiting for the rates on other things

Input and output (I / O) operation and the hard disk or a network, especially when the processor is pretty slow compared to the code can run faster. As a result, the read / write operations can be freely blocked pending completion.

Input Line in java.io package of classes () reads the block until the byte stream or an IOException is thrown in the method until you have read. This method can not do anything different execution threads in a stream of bytes, in anticipation of the arrival. If multiple threads are created, one thread is blocked waiting for input, another thread can perform other activities.

, GUI event handling thread of the division of labor between the two threads that other users are free to the event to process. In particular, you might want another button called Cancel request, the server communicates with the worker thread signals will cancel. Do you interact with the server, but if the workers do not use threads, the GUI event thread was simply impossible to work to cease activities on the occasion of the Cancel button to ask.

Simplify the object model

It is a system designed for the needs of some of the objects, which can lead to threads running through their built-in object-oriented analysis before. Oppose such activities, as opposed to ‘em I can stand. Change passive object only if the internal state of a road is named. Active object can change the internal state of autonomy.

Leave a Reply

Blog Home