Differences between Asynchronization and Multithreading

Source: Internet
Author: User

I. What is the difference between Asynchronization and multithreading? In fact, Asynchronization is the purpose, while multithreading is the method to achieve this purpose. Asynchronous means that after A initiates an operation (usually time-consuming operations, there is no need for asynchronous operations if the operation is not time-consuming), A can continue to handle its own affairs, you don't have to wait for this time-consuming operation to return .. This asynchronous programming model in. Net simplifies multi-threaded programming. We can do an asynchronous operation without even worrying about the Thread class.

2. With the popularization of multiple hard-threaded CPUs (hyper-threading and dual-core), the design methods for concurrent programs such as multithreading and asynchronous operations have also received more attention and discussion. This article focuses on how to use concurrency to maximize program performance.

  Similarities and differences between multithreading and asynchronous operations

Both multithreading and asynchronous operations can avoid calling thread blocking and improve software responsiveness. Sometimes we think that multithreading and asynchronous operations are equivalent. However, there are some differences between multithreading and asynchronous operations. These differences lead to the difference between the timing of using multithreading and asynchronous operations.

  Nature of asynchronous operations

All programs will eventually be executed by computer hardware. Therefore, to better understand the nature of asynchronous operations, we need to understand its hardware base.
Friends who are familiar with computer hardware must be familiar with the DMA term. The technical specifications of hard disks and optical drives all have clear DMA mode indicators. In fact, NICS, sound cards, and video cards also have DMA functions. DMA is straight

Memory Access means that hardware with DMA functions can exchange data with memory without consuming CPU resources. The hardware is enabled as long as the CPU sends a command during data transmission.

The hardware will trigger an interruption after the transmission is complete to notify the operation to complete. These I/O operations that do not consume CPU time are the hardware basis of asynchronous operations. So even in DOS
Such single-process (and wireless) systems can also initiate asynchronous DMA operations.

  Thread nature

A thread is not a computer hardware function, but a logic function provided by the operating system. A thread is essentially a piece of code that runs concurrently in a process, therefore, threads require the operating system to invest CPU resources for running and scheduling.

  Advantages and disadvantages of asynchronous operations

Because asynchronous operations do not require additional thread burden and Use callback for processing, the processing function does not need to use shared variables (even if it cannot be completely used, at least can be reduced
Less
The number of shared variables) to reduce the possibility of deadlocks. Of course, asynchronous operations are not perfect. The programming of asynchronous operations is highly complex. The program mainly uses the callback method for processing.
It is difficult to debug.

  Advantages and disadvantages of Multithreading

The advantages of multithreading are obvious. The processing program in the thread is still executed in sequence, which conforms to the thinking habits of ordinary people, So programming is simple. However, the disadvantages of multithreading are also obvious. The use (abuse) of threads will impose additional burden on context switching. In addition, shared variables between threads may cause deadlocks.

  Applicability

After understanding the advantages and disadvantages of threads and asynchronous operations, we can discuss the rational use of threads and asynchronous operations. In my opinion, when I/O operations need to be executed, asynchronous operations are better than thread + synchronization.
I/O operations are more appropriate. I/O operations include not only direct file and network read/write operations, but also database operations, Web services, HttpRequest, And. net
Remoting and other cross-process calls.

The applicability of threads is those that require long CPU operations, such as time-consuming graphics processing and Algorithm Execution. However
Due to the simplicity and habit of using thread programming, many friends often use threads to execute time-consuming I/O operations. In this way, when there are only a few concurrent operations
It is not appropriate to handle a large number of concurrent operations.

 

Asynchronous call and Multithreading

Asynchronous calling does not reduce the thread overhead. Its main purpose is to make the main thread that calls the method do not need to wait synchronously on this function call,
So that the main thread can continue to execute the code below it. At the same time,
The system takes a thread from the ThreadPool to execute. This helps us send the data we want to write/read to the NIC. Because we don't need to wait, we have done two things at the same time.
This effect is the same as starting another thread to execute write operations in the waiting mode. However, asynchronous threads can use the thread pool of the Operating System/. Net,
The system can dynamically manage the size of the thread pool based on the throughput.

========================================================== ====================================
Asynchronous and multithreading: in terms of the dialectical relationship, asynchronous and multithreading have the same relationship from time to time. Asynchronization is the purpose. multithreading is just a means for us to implement Asynchronization. what is asynchronous: asynchronous means that when a call request is sent to the called user, the caller does not have to wait for the result to be returned. to implement Asynchronization, you can use multithreading technology or hand it over to another process for processing.
========================================================== ======================================

The implementation method of the thread pool is different from that of the thread pool. the thread in the thread pool during initialization is 0. when a process requires a thread, create a thread to execute the user's method. note that this thread
After the execution is completed, it is not destroyed immediately, but suspended for waiting. If other methods need to be executed, the system will wake up for processing. only when it waits for 40 seconds (there is no official record, it may be another number) and No task is executed
In the thread pool, if the threads in the thread pool are not enough to process the task, a new thread will be created for execution.

In asynchronous mode, common threads are used, and system functions are called asynchronously. Some IO operations are asynchronous, but they may not need a thread to run. For example, if the hardware has the DMA function
When DMA transmits data, the CPU does not need to process the data. You only need to initiate the transmission and wait until the transmission ends. Specific to the. net platform, such as Socket BeginSend, if
It is a platform running after Windows 2000. At the underlying layer, it will call the asynchronous completion port for sending.

Asynchronous execution in. Net actually uses asynchronous delegation. The method to be executed by the asynchronous delegate is submitted to the thread pool of. net, and the thread in the thread pool executes the Asynchronous Method.

Asynchronous execution must also be performed, but not in the current thread. Of course, it must be executed in another thread. Asynchronous usually uses the thread of the system thread pool, and the performance is generally better. (Because it can be used multiple times, it does not need to be reused during application.
To apply for a new thread, you only need to retrieve it from the pool .) Asynchronization is an effect, while multithreading is a specific technology. It can be said that "Asynchronous" is implemented using "multithreading ".

Asynchronous and multithreading are two different concepts and cannot be compared in this way. asynchronous requests are generally used for IO and other time-consuming operations. The advantage is that function calls return immediately and corresponding worker threads return immediately to the system for reuse.
Because the system's thread resources are very valuable, there are usually a certain number of restrictions, such as. net is 25 by default. If the asynchronous method is used, use these fixed threads to serve more requests within a fixed period of time.
If synchronous mode is used, each request occupies this thread from start to end, and the number of requests that the server can serve at the same time is reduced. After the asynchronous operation is completed, the system selects a ticket from the available thread.
The row callback program. At this time, this thread may be the thread that was just sending the request, or another thread, because the system selects the thread randomly, so it cannot be said that it is definitely not the first thread.
Multithreading is used to concurrently execute multiple tasks.

However, there is a problem that asynchronous sometimes has a higher priority than the main thread. This feature is different from multithreading.

Asynchronous calling does not reduce the thread overhead. Its main purpose is to make the main thread that calls the method do not need to wait synchronously on this function call,
So that the main thread can continue to execute the code below it. At the same time,
The system takes a thread from the ThreadPool to execute. This helps us send the data we want to write/read to the NIC. Because we don't need to wait, we have done two things at the same time.
This effect is the same as starting another thread to execute write operations in the waiting mode. However, asynchronous threads can use the thread pool of the Operating System/. Net,
The system can dynamically manage the size of the thread pool based on the throughput.

========================================================== ====================================
Asynchronous and multithreading: in terms of the dialectical relationship, asynchronous and multithreading have the same relationship from time to time. Asynchronization is the purpose. multithreading is just a means for us to implement Asynchronization. what is asynchronous: asynchronous means that when a call request is sent to the called user, the caller does not have to wait for the result to be returned. to implement Asynchronization, you can use multithreading technology or hand it over to another process for processing.
========================================================== ======================================

The implementation method of the thread pool is different from that of the thread pool. the thread in the thread pool during initialization is 0. when a process requires a thread, create a thread to execute the user's method. note that this thread
After the execution is completed, it is not destroyed immediately, but suspended for waiting. If other methods need to be executed, the system will wake up for processing. only when it waits for 40 seconds (there is no official record, it may be another number) and No task is executed
In the thread pool, if the threads in the thread pool are not enough to process the task, a new thread will be created for execution.

In asynchronous mode, common threads are used, and system functions are called asynchronously. Some IO operations are asynchronous, but they may not need a thread to run. For example, if the hardware has the DMA function
When DMA transmits data, the CPU does not need to process the data. You only need to initiate the transmission and wait until the transmission ends. Specific to the. net platform, such as Socket BeginSend, if
It is a platform running after Windows 2000. At the underlying layer, it will call the asynchronous completion port for sending.

Asynchronous execution in. Net actually uses asynchronous delegation. The method to be executed by the asynchronous delegate is submitted to the thread pool of. net, and the thread in the thread pool executes the Asynchronous Method.

Asynchronous execution must also be performed, but not in the current thread. Of course, it must be executed in another thread. Asynchronous usually uses the thread of the system thread pool, and the performance is generally better. (Because it can be used multiple times, it does not need to be reused during application.
To apply for a new thread, you only need to retrieve it from the pool .) Asynchronization is an effect, while multithreading is a specific technology. It can be said that "Asynchronous" is implemented using "multithreading ".

Asynchronous and multithreading are two different concepts and cannot be compared in this way. asynchronous requests are generally used for IO and other time-consuming operations. The advantage is that function calls return immediately and corresponding worker threads return immediately to the system for reuse.
Because the system's thread resources are very valuable, there are usually a certain number of restrictions, such as. net is 25 by default. If the asynchronous method is used, use these fixed threads to serve more requests within a fixed period of time.
If synchronous mode is used, each request occupies this thread from start to end, and the number of requests that the server can serve at the same time is reduced. After the asynchronous operation is completed, the system selects a ticket from the available thread.
The row callback program. At this time, this thread may be the thread that was just sending the request, or another thread, because the system selects the thread randomly, so it cannot be said that it is definitely not the first thread.
Multithreading is used to concurrently execute multiple tasks.

However, there is a problem that asynchronous sometimes has a higher priority than the main thread. This feature is different from multithreading.

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.