Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between async and multithreading in C #

Shulou Source: shulou.com Published: 2022-05-31 17:51:55 10月25日 Update

This article introduces the knowledge of "what is the difference between async and multithreading in C#". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. differences and connections

What's the difference between async and multithreading? In fact, asynchronism is the goal, and multithreading is the way to achieve this. Async means that after An initiates an operation (usually a time-consuming operation, there is no need for asynchrony if it is not time-consuming), it can continue to deal with its own business without waiting for the time-consuming operation to return. This asynchronous programming model in .net simplifies multithreaded programming, and we can do an asynchronous operation without even caring about the Thread class.

Async sometimes uses ordinary threads and 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 calling DMA to transfer data, CPU does not need to perform processing, just need to initiate the transmission and wait for the end of the transmission. Specific to the. Net platform, such as Socket's BeginSend, if it is running on a platform after Windows 2000, the asynchronous completion port will be called at the bottom to send it.

Asynchronous execution in .net actually uses asynchronous delegates. 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.

II. Scope of application

It is more appropriate to use asynchronous operations when you need to perform an Iwhite O operation. ICompo operation includes not only direct reading and writing of files and networks, but also cross-process calls such as database operations, Web Service, HttpRequest and. Net Remoting.

The scope of application of threads is the kind of situations that require long-time CPU operations, such as time-consuming graphics processing and algorithm execution.

Third, an example of async

As you probably know, using delegate can "automatically" enable a method to be called asynchronously. Intuitively, I think the compiler or CLR uses another thread to execute the target method. Is this the case or not? Let's prove it with a piece of code.

Delegate void AsyncFoo (int I); static void Main (string [] args) {PrintCurrThreadInfo ("Main ()"); for (int I = 0; I < 10; iTunes +) {PostAsync ();} Console.ReadLine () } / output the current thread's information / method name static void PrintCurrThreadInfo (string name) {Console.WriteLine ("ThreadId of" + name + "is:" + Thread.CurrentThread.ManagedThreadId + ", current thread is" + (Thread.CurrentThread.IsThreadPoolThread? "": "not") + "thread pool thread.") } / deliver an asynchronous call / static void PostAsync () {AsyncFoo caller = new AsyncFoo (Foo); caller.BeginInvoke (1000, new AsyncCallback (FooCallBack), caller);} / Test method, Sleep certain time / Sleep time static void Foo (int I) {PrintCurrThreadInfo ("Foo ()"); Thread.Sleep (I);} static void FooCallBack (IAsyncResult ar) {PrintCurrThreadInfo ("FooCallBack ()") AsyncFoo caller = (AsyncFoo) ar.AsyncState; caller.EndInvoke (ar);}

The output of this code is as follows:

Thread Id of Main () is: 1, current thread is not thread pool thread.Thread Id of Foo () is: 3, current thread is thread pool thread.Thread Id of FooCallBack () is: 3, current thread is thread pool thread.Thread Id of Foo () is: 3, current thread is thread pool thread.Thread Id of Foo () is: 4, current thread is thread pool thread.Thread Id of Foo () is: 5, current thread is thread pool thread.Thread Id of FooCallBack () is: 3, current thread is thread pool thread.Thread Id of Foo () is: 3 Current thread is thread pool thread.Thread Id of FooCallBack () is: 4, current thread is thread pool thread.Thread Id of Foo () is: 4, current thread is thread pool thread.Thread Id of Foo () is: 6, current thread is thread pool thread.Thread Id of FooCallBack () is: 5, current thread is thread pool thread.Thread Id of Foo () is: 5, current thread is thread pool thread.Thread Id of Foo () is: 7, current thread is thread pool thread.Thread Id of FooCallBack () is: 3 Current thread is thread pool thread.Thread Id of Foo () is: 3, current thread is thread pool thread.Thread Id of FooCallBack () is: 4, current thread is thread pool thread.Thread Id of FooCallBack () is: 6, current thread is thread pool thread.Thread Id of FooCallBack () is: 5, current thread is thread pool thread.Thread Id of FooCallBack () is: 7, current thread is thread pool thread.Thread Id of FooCallBack () is: 3, current thread is thread pool thread.

As you can see from the output, the asynchronous calls that A. net uses delegate to "automatically" generate are using a different thread (and a thread pool thread).

This is the end of the content of "what's the difference between async and multithreading in C#". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Threads methods output code time transmission processing caching no content function platform data time more purpose knowledge scope scope delegation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MariaDB Microsoft Linux NVidia