. NET detailed Asynchronous programming

Source: Internet
Author: User

1. Introduction

Before talking about Asynchrony, let's start with a few confusing concepts, parallel, multithreaded, and asynchronous.

parallelism , generally referred to as parallel computing, is that there are multiple instructions at the same time being executed, which may be executed on multiple cores on the same CPU, or on multiple CPUs, or multiple physical hosts or even multiple networks.

multithreading , in general, refers to multiple threads in the same process (including their data structures, contexts, and code snippets) running cooperatively. Multiple threads in a multicore computer will have the opportunity to run on more than one core at the same time, and if the thread is doing a calculation, the rows are calculated in parallel.

asynchronous , in contrast to synchronization, refers to the call after another operation, does not wait for its results, continue after the operation, if there is no other action, the current thread will go to sleep, and CPU time will have the opportunity to cut to other threads. Gets the notification and result in the form of a callback function after the asynchronous operation completes. There are several implementations of async, such as multi-threading and completion ports. Multithreading takes an asynchronous operation into another thread, gets a completion notification through a polling or callback method, completes a port, takes over the dispatch of an asynchronous operation by the operating system, and triggers a callback method on completion by a hardware interrupt, which does not require an additional thread.

This article discusses. NET, and many asynchronous patterns that occur during its evolution.

2. Synchronization
 Public voidMain () {Stopwatch Stopwatch=NewStopwatch (); Stopwatch.            Start ();            Mainmethod ();            Elapsedtimemethod (); Stopwatch.            Stop (); varresult =stopwatch.            Elapsedmilliseconds;        Console.WriteLine (result); }         Public voidMainmethod () {Thread.Sleep ( +); }        /// <summary>        ///time-consuming approach: simulate time-consuming operations such as Io,http/// </summary>        /// <returns></returns>         Public voidElapsedtimemethod () {Thread.Sleep ( the); }
View Code

The result is more than 4 seconds.

3. Asynchronous 3.1 Thread era
 Public voidMain () {Stopwatch Stopwatch=NewStopwatch (); Stopwatch.            Start (); //Open Two ThreadsThread mainthread=NewThread (Mainmethod); Thread Elaptread=NewThread (Elapsedtimemethod);            Mainthread.start ();            Elaptread.start (); //block two threads and wait for all two threads to completeMainthread.join ();            Elaptread.join (); Stopwatch.            Stop (); varresult =stopwatch.            Elapsedmilliseconds;        Console.WriteLine (result); }         Public voidMainmethod () {Thread.Sleep ( +); }        /// <summary>        ///time-consuming approach: simulate time-consuming operations such as Io,http/// </summary>        /// <returns></returns>         Public voidElapsedtimemethod () {Thread.Sleep ( the); }
View Code

The result: More than 3 seconds, here is a brief introduction, if you are interested in Treadpool, please search in the garden yourself.

3.2 Task era
         Public voidMain () {Stopwatch Stopwatch=NewStopwatch (); Stopwatch.            Start (); varTask1 = Task.run (() ={Task1 ();            }); varTask2 = Task.run (() ={Task2 ();            }); Task1.            Wait (); Task2.            Wait (); Stopwatch.            Stop (); varresult =stopwatch.            Elapsedmilliseconds;        Console.WriteLine (result); }         Public voidTask1 () {Task.delay ( -); }         Public voidTask2 () {Task.delay (4000); }
View Code

The result is: 4 seconds

3.3 Asnyc await times
[TestMethod] Public AsyncTask Main () {Stopwatch Stopwatch=NewStopwatch (); Stopwatch.            Start (); awaitTask1 (); awaitTask2 (); Stopwatch.            Stop (); varresult =stopwatch.            Elapsedmilliseconds;        Console.WriteLine (result); } [TestMethod] Public AsyncTask Main1 () {Stopwatch Stopwatch=NewStopwatch (); Stopwatch.            Start (); varA=Task1 (); varb=Task2 (); awaitA; awaitb; Stopwatch.            Stop (); varresult =stopwatch.            Elapsedmilliseconds;        Console.WriteLine (result); }         Public AsyncTask Task1 () {awaitTask.delay ( -); }         Public AsyncTask Task2 () {awaitTask.delay (4000); }
View Code

Main result is 6;main1 result is 4; The reason is not to mention it; haha main is equivalent to synchronization.

 

. NET detailed Asynchronous programming

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.