Java Thread Programming-Why use multithreading (why use multiple thread)

Source: Internet
Author: User

In many cases, it is helpful to use multithreading in a program. Below are some in-depth observations about why it is advantageous.

Better interaction with users (better interaction with the user)
If there is only one thread, one program can only do one thing at a time. Take the word processing program as an example. How nice it is to open the second document when the first document is being formatted and queued for printing. In some previous word processing software, when a user wants to print a document, if the document is preparing to be sent to the printer, he has to wait. (That is, users cannot do other things while waiting for printing) Modern word processing software development uses multithreading to do two things at the same time. In a single-processor system, you can use the operating system to quickly switch back and forth between two tasks to simulate this scenario, in order to achieve better interaction with users.

From the microprocessor perspective, even the fastest typist spends a lot of time between keys. In these large time gaps, the processor can process other tasks. When another thread stops other jobs, if a thread is always waiting for a quick response to the user's actions, such as clicking the mouse or pressing the key, users will feel better responses from the system.


Simulation of simultaneous activities)


Even if there is only one processor, the threads in Java seem to run concurrently. The processor runs each thread for a short period of time and switches between threads to simulate concurrent execution. This makes every thread seem to have its own processor. With this feature, you can make multiple tasks seem to be happening at the same time. In fact, before the context switches to another thread, each thread only runs for a short time.

Exploitation of Multiple Processors)
Some machines have multiple processors. If the implementation and development of the current operating system and JVM utilize multi-processor, multi-threaded Java programs can implement real concurrent thread execution. The Java program does not need to be modified because it uses threads. Just as these threads run on different processors at the same time, they can only run faster.


When waiting for slow I/O operations, you can do other things (do other things while waiting for slow Io operations)


Compared with the code execution speed in the processor, the disk input and output operations, especially cross-network operations, are relatively slow. The read/write operations may be blocked for a long time to wait for completion.

In the java. Io package, an inputstream-like read () method is blocked until a byte is read from the stream or an ioexception is thrown. When the byte of the stream arrives, the thread that executes this method cannot do other things. If multiple threads are created, other threads can complete other activities after a thread is blocked.

For example, you have a Java applet that contains multiple textfield components and shows how two threads provide users with better user interaction pseudocode. The first thread is a GUI event thread, Which is blocked most of the time in the waifornextevent () method. The second thread is the working thread, Which is initialized to the blocking state and waits for a signal to continue working in the waituntilsignalled () method. After all textfield components are generated, the user clicks the transfer data button. The GUI event thread is deblocked and enters the delivereventtolistenser () method, and then calls the actionreceivmed () method. This method sends a signal to the working thread and immediately returns the waitfornextevent () method. The working thread is removed from the blocking and the waituntilsignaled () method is left, And the gatherdataandtransmit () method is entered. The worker thread collects and transmits user input data, and is blocked when waiting for the server to confirm the information. After reading the confirmation information, the worker thread returns the waituntilsignalled () method.


Simplify Object Modeling)


When building a system, you need to analyze the system according to the object-oriented method, which may require some objects to run in the thread mode. Such objects can be considered active and correspond to passive objects. A negative object can modify its internal State only when its method is called. A positive object can automatically change its internal status.

For example, you can build a graphical component of a digital clock to display the hour and minute of the current system. Every 60 seconds, the minute in the component changes. The simplest design is to create a thread in the clock component, which is responsible for modifying numbers. If this is not the case, an external thread is required. In addition to other tasks, it also needs to constantly monitor whether numbers need to be updated. If the External Thread needs to read data from inputstream, it will be blocked. What if it waits for a byte for more than one minute? So here, we can use the advantages of multi-threaded programming to simplify the problem solving.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.