Advantages and costs of Multithreading

Source: Internet
Author: User

Despite many challenges, multithreading has some advantages that keep it used. These advantages are:

  • Better resource utilization

  • Program Design is simpler in some cases

  • Faster program response

Better resource utilization

Imagine an application reading and processing files from a local file system. For example, it takes 5 seconds to read a file from a disk and 2 seconds to process a file. To process two files, you must:

5 seconds to read files A2 seconds to process files A5 seconds to read files B2 seconds to process files B ------------------- a total of 14 seconds

When reading files from a disk, most of the CPU time is used to wait for the disk to read data. During this period, the CPU is very idle. It can do something else. By changing the operation sequence, you can better use CPU resources. See the following sequence:

5 seconds to read files A5 seconds to read files B + 2 seconds to process files A2 seconds to process files B ----------------- a total of 12 seconds

The CPU waits until the first file is read. Then read the second file. When the second file is read, the CPU will process the first file. Remember, the CPU is idle most of the time while waiting for the disk to read files.

In general, the CPU can do some other things while waiting for IO. This is not necessarily the disk IO. It can also be network I/O or user input. In general, the IO of the network and disk is much slower than that of the CPU and memory.

Easier Program Design

In a single-threaded application, if you want to write a program to manually process the reading and processing sequence mentioned above, you must record the reading and processing status of each file. Instead, you can start two threads to process the reading and operation of a file. The thread is blocked while waiting for the disk to read files. While waiting, other threads can use the CPU to process the files that have been read. The result is that the disk is always busy reading different files into the memory. This will increase the disk and CPU usage. In addition, each thread only needs to record one file, so this method is also easy to program.

Faster program response

Another common purpose of turning a single-threaded application into a multi-threaded application is to achieve a faster response. Suppose a server application listens for incoming requests on a port. When a request arrives, it processes the request and then returns it to the listener.

The server process is as follows:

 
 
  1. while(server is active){ 
  2.     listen for request 
  3.     process request 

If a request takes a large amount of time for processing, new clients cannot send requests to the server during this period. Requests can be received only when the server is listening. Another design is that the listening thread transmits the request to the worker thread, and then immediately returns to the listener. The worker thread can process this 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 

In this way, the server thread quickly returns to listen. Therefore, more clients can send requests to the server. This service also becomes more responsive.

The same applies to desktop applications. If you click a button to start running a time-consuming task, the thread needs to execute the task and update the window and button, during the task execution, the application process does not seem to have the same response. On the contrary, tasks can be passed to the worker thread word thread ). When the worker thread is busy processing tasks, the window thread can freely respond to requests from other users. When the worker thread completes the task, it sends a signal to the window thread. The window thread can update the application window and display the task results. For users, this kind of program with a thread design seems to respond faster.

From a single-threaded application to a multi-threaded application, it not only brings benefits, but also has some costs.Do not use multithreading only to use multithreading. It should be clear that multithreading is used only when the advantages of multithreading are higher than the cost. If you have any questions, you should try to measure the application's performance and response capabilities, not just speculation.

More complex design

Although some multi-threaded applications are simpler than single-threaded applications, others are generally more complex. This part of code requires special attention when multiple threads access Shared data. The interaction between threads is often very complicated. Errors generated by incorrect thread synchronization are very difficult to detect and reproduce to fix.

Overhead of context switching

When the CPU switches from the execution thread to the execution of another thread, it needs to first store the local data of the current thread, program pointer, and so on, and then load the local data of the other thread, program pointer. This type of switch is called "context switch "). The CPU executes a thread in one context and switches to another context to execute another thread.

Context switching is not cheap. If not necessary, context switching should be reduced.

Increase resource consumption

When a thread is running, it needs to obtain some resources from the computer. In addition to the CPU, the thread also needs some memory to maintain its local stack. It also needs to occupy some resources in the operating system to manage threads. We can try to write a program and let it create 100 threads. These threads do nothing, just waiting, and then check how much memory the program occupies during running.

Http://ifeve.com/benefits/.

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.