Atitit. History of asynchronous programming: 1.1. & #160; TAP & #160; & amp; & #160; async/await, atitit1.1.

Source: Internet
Author: User
Tags apm

Atitit. History of asynchronous programming 1.1. TAP & async/await, atitit1.1.

Atitit. History of asynchronous programming 1.1. TAP & async/await

 

 

 

1. History of asynchronous programming 1

1.1. Thread1

1.2. Task1

1.3. Async await2

1.3.1. await Prototype 2

2. Common asynchronous modes include callbaks, listeners (an observer mode), promise, and 3.

3. There are four methods to implement asynchronous programming. These four types of access requests correspond to four asynchronous calling modes, which are divided into two categories: "Waiting" and "Callback. 4

3.1. APM4

3.2. EAP5

3.3. TAP & async/await5

3.4. async/await the two newly added keywords are only syntactic sugar, and the compiled code does not create a thread 5

4. Refer to 6

 

 

1. Development History of asynchronous programming 1.1. Thread

 

1.2. Task

 

 

Return Value

Thead does not return values, but as a more advanced Task, you must make up for this function.

1. static void Main (){

2. // GetDayOfThisWeek runs in another thread

3. var dayName = Task. Run <string> () = >{ return GetDayOfThisWeek ();});

4. Console. WriteLine ("today is: {0}", dayName. Result );

5 .}

 

 

1.3. Async await

 

I don't know how to handle it. Today, let's take a good look at the differences and characteristics between the two brothers and their uncles (tasks) and their grandfathers (threads). This article will discuss the differences between Thread and Task. NET 4.5 async and await. Parallel Programming in these three modes provides a general introduction including: enabling the thread, returning the thread results, suspending the thread, and exception handling in the thread.

 

By default, tasks directly use the Thread pool, but threads do not. If we do not use tasks and want to use thread pools, we can use the ThreadPool class.

 

Author: nickname: old wow's paw (full name: Attilax Akbar Al Rapanui Attila Akba Arla Panui) Name: AI long, EMAIL: 1466519819@qq.com

Reprinted please indicate Source: http://www.cnblogs.com/attilax/

 

 

1.3.1. await prototype

Execution sequence after await

Thanks to the correction of loocus, await will not start a new thread (await will never start a new thread), so there is a problem with the figure above.

Await will not start a new thread, and the current thread will keep going until a real Async method (such as HttpClient. getStringAsync), the internal method will use the Task. run or Task. factory. startNew to enable the thread. That is to say, if the method is not the Async method provided by. NET, we need to create the Task ourselves to realize

Await is not for the async method, but for the Task returned to us by the async method. This is why all async methods must be returned to our Task. Therefore, we can also add the await keyword before the Task. This tells the compiler that I need to wait for the return value of the Task or wait until the Task is executed.

 

The Task. GetAwait () method returns an awaitable object to us. By calling the GetResult method of this object, the main thread will be suspended. Of course, not all cases will be suspended. Do you still remember the features of our Task? At the beginning, another thread was started to execute the Task. When we call the result of the Task, if the Task has been completed, the main thread does not have to wait to get its results directly. If the execution is not completed, the main thread has to wait.

Await is actually calling the GetResult method of the awaitable object.

1./here the main thread will wait until the task is executed and we get the returned result.

2. var result = task. GetAwaiter (). GetResult ();

3. // the wait will not be suspended here, because the task has been executed and we can get the result directly.

4. var result2 = await task;

 

 

 

 

 

Java has a class called java. util. concurrent. Future which is equivalent to the C # Task class.

You can set off work on a java. util. concurrent. executor object. there are too implementations but ForkJoinTask. fork () is worth a look if you hit thread limits as soon as you try this.

You will be given a Future when you start work. your method will carry on running. when you need the result from your Future you call get () and it will block until the result is ready. this is similar to using the await keyword in C #.

2. Common asynchronous modes include callbaks, listeners (an observer mode), promise,

Listeners is also a common asynchronous programming method, such:

$ Elem. on ('click', doClick); function doClick () {// do something };

This method is called the command mode in the design mode. The advantage of this method is that you can register multiple events and call the registered function when the event is triggered.

In summary, Promise asynchronous programming implements thenable. To put it bluntly, I am a good friend with you. I may need your help. You can wait at any time, if necessary, I will notify you immediately. You and Mr. Wang are also good buddies, and tell you that you need help and inform him immediately. Of course, you will notify him after I notify you. Use pseudocode:

Myself (function () {callXiaoLi ();}). then (function () {// I am Xiao Li. When you call me, I will call Xiao Wang callXiaowang ();}). then (function () {console. log ('I am Mr. Wang ');});

Then implementation is relatively complex and needs to be explained, for example:. then (xx). then ()

Each time. then is generated, a new promise object is generated, that is, the next then is registered with the previous promise object, and its departure condition depends on the promise generated by the previous. then object.

If the last result returned by. then is a non-promise object (no then method ),

If the previous. the final result returned by then is the promise object, that. the promise generated by then must depend on the promise in the returned result and the promise in the inner layer. then (". the promise generated during then ". resolve)

3. There are four methods to implement asynchronous programming. These four types of access requests correspond to four asynchronous calling modes, which are divided into two categories: "Waiting" and "Callback.

 

3.1. APM

The earliest Asynchronous Programming mode of C #. NET was called APM (Asynchronous Programming Model ). This mode is mainly composed of a pair starting with Begin/End. The BeginXXX method is used to start a time-consuming operation (code segment that requires asynchronous execution)

3.2. EAP

In C #. in the second version of NET, a new Asynchronous programming model EAP (Event-based Asynchronous Pattern) is added. In the Asynchronous code of the EAP mode, A typical feature is an Async end method and a Completed end event. The XXXCompleted event will be triggered when asynchronous processing is completed, and the result of asynchronous method can be operated in the event processing function. In the EAP code, the CancelAsync method is used to cancel asynchronous operations, and an event ending with ProgressChenged is used to report the operation progress. In this way, cancellation and progress reporting are more advantageous for EAP than APM. Based on the introduction of TAP in the following article, you will find that the cancellation mechanism in EAP has no continuity and is not very common.

3.3. TAP & async/await

Starting from. NET4.0, a new library named TPL is mainly responsible for processing asynchronous and parallel operations. The goal is to provide a unified operation interface for asynchronous and concurrent operations. The core of the TPL library is the Task class. When a Task is created, it almost does not have to deal with underlying classes such as Thread as the asynchronous and Concurrent Versions of the previous version. As a user, we only need to deal with the Task well, there is a class named TaskScheduler behind the Task to process the execution of the Task on the Thread. In this case, TaskScheduler and Task are the basis for asynchronous and concurrent operations in. NET4.0, which is the best choice for coding.

3.4. The two newly added keywords async/await are only syntactic sugar, and the compiled code does not create a thread.

Await will not start a new thread, and the current thread will keep going until a real Async method (such as HttpClient. getStringAsync), the internal method will use the Task. run or Task. factory. startNew to enable the thread. That is, if the method is not the Async method provided by. NET, we need to create the Task ourselves to create the thread.

4. Reference

Async & await's past and present-51CTO.COM.htm

The three different programming languages in your hand: callbacks,listeners,promise_javascript_7 .htm

. NET asynchronous programming Summary-four implementation modes-DebugLZQ-bokyue.htm

 

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.