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, 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 startI/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 moreCPU 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 savingsCPU Resources.

notes:DMA(Direct Memory
Access) Direct memory access, Seeing the name of a thing one thinks of its functionDMA The function is to allow the device to bypass the processor, Read data directly from memory. Data exchange through direct memory access is almost losslessCPU Resources. 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 releaseCPU Pressure.

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; }
} }
UseWait() andGetAwaiter().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();// staymain Wait inasync Method execution complete int i =
MyMethod().GetAwaiter().GetResult();// Used inmain Get synchronously inasync 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; } } }