"Java Multi-Threading benefits"

Source: Internet
Author: User

Original: http://tutorials.jenkov.com/java-concurrency/benefits.html

Jakob Jenkov translation: Ancient Sheng Chang proofreading: Ouzhengkong

Despite many challenges, multithreading has some advantages in Java learning that make it always used. These advantages are:

    • Better resource utilization
    • Programming is easier in some cases
    • Faster program Response


Better Resource Utilization

Imagine a scenario in which an application needs to read and process files from a local file system. For example, it takes 5 seconds to read a file from disk and 2 seconds to process a file. Processing two files requires:

  1. 5 Seconds to read a file
  2. 2 sec Processing File a
  3. 5 Second Read File B
  4. 2 sec Processing File B
  5. ---------------------
  6. It takes 14 seconds altogether.
Copy Codewhen reading files from disk, most of the CPU time is used to wait for the disk to read the data. During this time, the CPU is very idle. It can do something else. By changing the order of operations, you can use CPU resources better. Look at the following order:
  1. 5 Seconds to read a file
  2. 5 sec Read File B + 2 sec processing file a
  3. 2 sec Processing File B
  4. ---------------------
  5. It takes 12 seconds altogether.
Copy Code

The CPU waits for the first file to be read out. It then starts reading the second file. When the second file is read, the CPU processes the first file. Remember that the CPU is idle most of the time while waiting for the disk to read the file.

Generally speaking, the CPU can do something else while waiting for IO. This is not necessarily disk IO. It can also be a network IO, or user input. Typically, network and disk IO are much slower than CPU and memory IO.



Easier Programming

In a single-threaded application, if you want to write a program to manually handle the read and processing sequences mentioned above, you must record the status of each file read and processed. Instead, you can start two threads, each of which handles reading and manipulating a file. The thread is blocked while waiting for the disk to read the file. While waiting, other threads are able to use the CPU to process files that have already been read. As a result, the disk is always busy reading different files into memory. This can lead to increased disk and CPU utilization. And each thread only needs to log one file, so this is also easy to implement programmatically.

Faster Program ResponseAnother common purpose of turning a single-threaded application into a multithreaded application is to implement a faster-responding application. Imagine a server application that listens for incoming requests on a certain port. When a request arrives, it handles the request and then returns to the listener.

The process of the server is described below:

  1. While (server is active) {
  2. Listen for request
  3. Process Request
  4. }
Copy CodeIf a request takes a significant amount of time to process, the new client cannot send the request to the server during this time period. Requests can be received only when the server is listening. Another design is that the listener thread passes the request to the worker thread, and then immediately returns to the listener. The worker thread is able to handle the request and send a reply to the client. This design is described as follows:
  1. While (server is active) {
  2. Listen for request
  3. Hand request to worker thread
  4. }
Copy Code

In this way, the service thread quickly returns to the listener. As a result, more clients can send requests to the server. This service also becomes more responsive.

The same is true for desktop applications. If you click on a button to start running a time-consuming task that is both performing the task and updating the window and button, the application does not seem to react as it does during the task execution. Instead, tasks can be passed to worker threads (Word thread). When a worker thread is busy processing a task, the window thread is free to respond to requests from other users. When the worker thread finishes the task, it sends a signal to the window thread. The window thread can update the application window and display the results of the task. For the user, this program with worker threading design appears to respond faster.

"Java Multi-Threading benefits"

Related Article

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.