1, What is asynchronous ?


Asynchronous operations are often used to perform tasks that may take a long time to complete , Such as opening a large file , Connecting to a remote computer or querying a database = Asynchronous operations are performed in a thread other than the main application thread . When an application calls a method to perform an operation asynchronously , Applications can continue to execute while asynchronous methods perform their tasks .

2, The difference between synchronous and asynchronous

synchronization (Synchronous): When performing an operation , The application must wait for the operation to complete before continuing .

asynchronous (Asynchronous): When performing an operation , Applications can continue to execute while asynchronous operations are executing . essence : Asynchronous operation , New thread started , Parallel execution of main thread and method thread .

3, The difference between asynchronous and multithreading

We already know ,
Asynchronous and multithreading are not the same , Asynchronous is the ultimate goal , Multithreading is just a way for us to realize asynchrony . Asynchrony is when a call request is sent to the callee , The caller can do other things without waiting for the result to return . Asynchrony can be implemented by multithreading or by other processes .

In short, it's : Asynchronous threads are managed by the thread pool , And multithreading , We can control it ourselves , Of course, in multithreading, we can also use thread pool .


As far as Internet pickpockets are concerned , If asynchronous mode is used to implement , It uses thread pools for management . When asynchronous operation is executed , The operation will be dropped to a worker thread in the thread pool to complete . When it starts I/O During operation , Asynchronous returns worker threads to the thread pool , This means that the work of getting the web page will not occupy any more CPU Resources . Until asynchronous completion , That is, the web page is obtained , Asynchronous will notify the thread pool through callback . so , Asynchronous mode with thread pool , Great savings CPU Resources for .

notes :DMA(Direct Memory
Access) Direct memory access , seeing the name of a thing one thinks of its function DMA The function is to allow the device to bypass the processor , Read data directly from memory . Data exchange through direct memory access is almost lossless CPU Resources for . In hardware , Hard disk , network card , sound card , Video card has direct memory access function . Asynchronous programming model is to make full use of the direct memory access function of hardware to release CPU Pressure of .

Application scenarios of both :

*
Computing intensive work , Adopt multithreading .

*
IO Intensive work , Adopt asynchronous mechanism .

C# Asynchronous code reference (async and await)
using System; using System.Threading; using System.Threading.Tasks; namespace
AsyncAwaitDemo { class Program { static void Main(string[] args) {
Console.WriteLine("main start.."); AsyncMethod(); Thread.Sleep(1000);
Console.WriteLine("main end.."); Console.ReadLine(); } static async void
AsyncMethod() { Console.WriteLine("start async"); var result = await
MyMethod(); Console.WriteLine("end async"); //return 1; } static async
Task<int> MyMethod() { for (int i = 0; i < 5; i++) { Console.WriteLine("Async
start:" + i.ToString() + ".."); await Task.Delay(1000); // Simulate time-consuming operations } return 0; }
} }
use Wait() and GetAwaiter().GetResult() Method to implement asynchronous method synchronous execution
using System; using System.Threading.Tasks; namespace AsyncTest { class
Program { static void Main(string[] args) { Console.WriteLine("Async Test
job:"); Console.WriteLine("main start..");
Console.WriteLine("MyMethod() Asynchronous method synchronous execution :");
MyMethod().Wait();// stay main Waiting in async Method execution complete int i =
MyMethod().GetAwaiter().GetResult();// For use in main Get synchronously in async Return result of method
Console.WriteLine("i:" + i); Console.WriteLine("main end..");
Console.ReadKey(true); } static async Task<int> MyMethod() { for (int i = 0; i
< 5; i++) { Console.WriteLine("Async start:" + i.ToString() + ".."); await
Task.Delay(1000); // Simulate time-consuming operations } return 0; } } }