The difference between async and multithreading
What is the difference between async and multithreading? In fact, asynchronous is the purpose, and multithreading is the way to achieve this. Asynchronous is to say, a after initiating an operation (generally is more time-consuming operations, if not time-consuming operation is not necessary to asynchronous), you can continue to self-processing its own things, do not wait for this time-consuming operation returned. NET of this asynchronous programming model, it simplifies multithreaded programming, we do not even have to care about the thread class, we can do an asynchronous operation out.
Second, with the popularization of multiple hard-threading CPUs (Hyper-threading, dual-core), the concurrent programming methods such as multithreading and asynchronous operation have been paid more attention and discussed. This article is mainly to explore how to use concurrency to maximize the performance of the program.
The similarities and differences between multithreading and asynchronous operations
Both multithreading and asynchronous operations can achieve the goal of avoiding calling thread blocking, thus improving the responsiveness of the software. Even sometimes we think that multithreading and asynchronous operations are the same concept. However, there are some differences between multithreading and asynchronous operations. These differences create the difference between the timing of using multithreading and asynchronous operations.
The nature of asynchronous operations
All programs will eventually be executed by the computer hardware, so in order to better understand the nature of the asynchronous operation, we need to understand its hardware base. Familiar with the computer hardware friend is certainly not unfamiliar with the word DMA, hard disk, optical drive technical specifications have a clear DMA mode indicator, in fact, the network card, sound card, video card also has DMA function. DMA is the meaning of direct memory access, which means that the DMA-capable hardware does not consume CPU resources when exchanging data with the memory. As long as the CPU sends an instruction when it initiates the data transfer, the hardware begins to exchange data between itself and the memory, and after the transfer is complete, the hardware triggers an interrupt to notify the operation to complete. These I/O operations that do not consume CPU time are the hardware basis for asynchronous operations. Therefore, even in a single process such as DOS (and the Wireless Path concept) system can also initiate asynchronous DMA operation.
The nature of Threads
A thread is not a function of a computer's hardware, but rather a logical function provided by the operating system, which is essentially a piece of code that runs concurrently in a process, so the thread requires the operating system to run and dispatch the CPU resources.
Advantages and disadvantages of asynchronous operations
Because asynchronous operations do not require additional thread burdens and are handled in a callback manner, the processing function can eliminate the possibility of deadlocks by eliminating the need for shared variables (even if it is not completely unused, at least by reducing the number of shared variables). Of course, asynchronous operations are not flawless. Writing asynchronous operations is a high degree of complexity, the program mainly uses callback method to handle, and ordinary people's way of thinking some first, and difficult to debug.
Advantages and disadvantages of multithreading
The advantages of multithreading are obvious, the handlers in the thread are still sequential execution, in line with the normal thinking habits, so programming is simple. However, the disadvantage of multithreading is equally obvious, the use of threads (misuse) will bring the burden of context switching to the system. Also, shared variables between threads can cause deadlocks to occur.
Scope of application
After understanding the pros and cons of threading and asynchronous operations, we can explore the logical use of a thread and async. I think that when I am required to perform I/O operations, it is more appropriate to use asynchronous operations than to use thread + synchronous I/O operations. I/O operations include not only direct files, read and write to the network, but also database operations, Web Service, HttpRequest, and cross-process calls such as. NET Remoting.
The scope of the thread is the kind that requires long CPU operations, such as long-time graphics processing and algorithm execution. But often because of the simple and consistent use of threading programming, many friends tend to use threads to perform long-time I/O operations. This is harmless when there are only a few concurrent operations, which is not appropriate if you need to handle a large number of concurrent operations.
Asynchronous calls and multithreading
The asynchronous invocation is not intended to reduce the overhead of the thread, and its primary purpose is to have the main thread of the calling method not need to wait on the function call synchronously, allowing the main thread to continue executing the code below it. At the same time, the system will be executed by taking a thread from ThreadPool, helping us to write/ The read data is sent to the network card. Since we do not need to wait, we are doing two things at the same time. This effect is the same as when you start another thread to perform the wait mode. However, an asynchronous thread can take advantage of the operating system/. NET thread pool, the system can dynamically manage the size of the thread pool based on throughput.
=======================================================================
Asynchronous and multi-threaded, from the dialectical relationship, asynchronous and multi-threaded and occasionally an equivalent relationship, asynchronous is the purpose, multithreading is just a means for us to implement Asynchrony. What is async: Async is when a call request is sent to the callee, The caller does not have to wait for the return of its result. Implementing async can be handled by multithreading or by handing over to another process
=========================================================================
Thread pool implementations are not the same as threads. The thread for online constructor when initializing is 0. When a process requires a thread, a thread is created that executes the user's method. It is important to note that this thread is not destroyed immediately after execution, but hangs waiting, if there are other methods that need to be executed, Wake-up processing. Only when it waits 40 seconds (no official records, possibly other numbers) to wake itself up without a task to execute and destroy itself, freeing up resources. Of course, if a thread in the thread pool is not enough to handle the task, a new thread is created to execute.
Asynchronous sometimes uses the normal thread, sometimes uses the system's asynchronous invocation function. Some IO operations are also asynchronous, but do not necessarily require a thread to run. For example: The hardware has DMA function, when the DMA transfer data, the CPU does not need to perform processing, only need to initiate the transmission and wait for the end of the transmission. specific to the. NET platform, such as the beginsend of the socket, if it is a platform that runs after Windows 2000, the asynchronous completion port is invoked at the bottom.
. The asynchronous execution in net is actually using an asynchronous delegate. Asynchronously delegates the method to be executed to the thread pool of. NET, which executes the async method by a thread in the thread pools.
Asynchronous execution has to be performed, not on the current thread, but on another thread. Async is typically used by threads in the system thread pool, typically with better performance. (because it can be used multiple times, the application does not need to reapply for a thread, just take it from the pool.) Asynchronous is an effect, multithreading is a specific technology. You can say, "multithreading" to achieve "asynchronous."
Async and multithreading are two different concepts that cannot be compared. Asynchronous requests are typically used in time-consuming operations such as IO, and his benefit is that function calls return immediately and the corresponding worker threads are immediately returned to the system for reuse. Because the thread resources of the system are very valuable, there is usually a certain number of restrictions, such as. NET default is 25. Asynchronously, with these fixed number of threads can serve more requests in a fixed time, and if synchronous, then each request will occupy this one thread, the server can service the number of requests at the same time less. When the asynchronous operation completes, the system picks an execution callback from the available thread, which may be the one that started the request, or the other thread, because the system picks the thread as random, so it cannot be said that it is not the first thread. Multithreading is used to perform multiple tasks concurrently.
One problem, however, is that async sometimes has a higher priority than the main thread. This feature is different from multithreading.
Java Asynchronous calls and multithreading