Atitit. & #160; Async & #160; await & #160; advantages and disadvantages & #160; asynchronous programming principles and implementation & #160; java & #160; c # & #160; php,

Source: Internet
Author: User

Advantages and disadvantages of Atitit. Async await asynchronous programming principles and implementation of java c # php,

Advantages and disadvantages of Atitit. Async await asynchronous programming principle and implementation of java c # php

 

1. async & await source 1

2. asynchronous programming history1

2.1. Thread Pool 2

2.2. Return Value 2

2.3. Semaphore 2

2.4. is the main thread capable of capturing thread exceptions? 2

3. await is not for the async method, but for the Task that the async method returns to us, 2

4. Java contains tasks and await3

5. --- code4

6. async & await disadvantages 4

7. ref5

 

 

1. Source of async & await

Completion of Asynchronous Operation Language levels... trend ..

2. asynchronous programming history

 

1. static void Main (){

2. new Thread (Go). Start (); // It is available at the beginning of. NET 1.0.

3. Task. Factory. StartNew (Go); // TPL is introduced in. NET 4.0.

4. Task. Run (new Action (Go); //. NET 4.5 adds a Run method.

5 .}

6.

7. public static void Go (){

8. Console. WriteLine ("I am another thread ");

9 .}

Note that after a Thread instance is created, you must manually call its Start method to Start it. But for a Task, StartNew and Run both create a new thread and start it immediately.

 

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/

 

2.1. Thread Pool

Thread creation is a resource-consuming task.. NET provides a thread pool to help us create and manage threads. 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.

 

2.2. Return Value

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

 

2.3. Semaphore

I really don't know how to translate this word. From the official explanation, we can understand it in this way. It can control the number of threads that access a piece of code or a resource. When the number of threads exceeds this limit, other threads will have to wait. Only after a thread is released, the following threads can be accessed. This function is similar to the lock, but is not exclusive. It allows a certain number of threads to access the lock at the same time.

2.4. is the main thread capable of capturing thread exceptions?

 

 

3. await is not for the async method, but for the Task that the async method returns to us,

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

 

This is why all async methods must be returned to the 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.

 

What is the difference between Task. GetAwaiter () and await Task?

 

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.

 

4. Java tasks and await

In Java, A futureTask goes through the task in 10. net ..

Java has no syntax-level await. It can only be implemented in the library mode ..

 

FutureTask async_task = taskService. async ()-> {

String echo1 = echoCmdResult_asStr (process. getInputStream ());

System. out. println ("-- echo1:" + echo1 );

Returnecho1;

}, 10 );

New Thread (async_task). start ();

System. out. println ("-- start get err stream" + filex. getUUidName ());

FutureTask async_task_err = taskService. async ()-> {

String echo2 = echoCmdResult_asStr (process. getErrorStream ());

System. out. println ("-- echo2:" + echo2 );

Returnecho2;

}, 10 );

New Thread (async_task_err). start ();

 

 

R = taskService. <String> await (async_task, 15, "$ output_timeout_ex ");

System. out. println ("-- finish await std stream" + filex. getUUidName ());

System. out. println ("----------------------------------------");

System. out. println ("-- start await err stream" + filex. getUUidName ());

String err = taskService. <String> await (async_task_err, 1, "$ err_stream_timeout_ex ");

 

 

5. --- code

 

Public FutureTask async (Callable object, int timeout_secs ){

FutureTask <String> task = new FutureTask <String> (object );

This. timeout_secs = timeout_secs;

Check_time_out (task );

Return task;

 

}

 

 

Public <t> t await (FutureTask <t> async_task, int timeout_secs2, Object defVal ){

Try {

Return async_task.get (timeout_secs2, TimeUnit. SECONDS );

} Catch (InterruptedException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

} Catch (ExecutionException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

 

} Catch (TimeoutException e ){

Async_task.cancel (true );

}

Return (t) defVal;

}

 

6. disadvantages of async & await

Only for the effect of the task type... not commonly used code ..

I had to use task + lambda to package a Haha...

 

FutureTask async_task_err = taskService. async ()-> {

String echo2 = echoCmdResult_asStr (process. getErrorStream ());

System. out. println ("-- echo2:" + echo2 );

Returnecho2;

}, 10 );

New Thread (async_task_err). start ();

 

String err = taskService. <String> await (async_task_err, 1, "$ err_stream_timeout_ex ");

 

7. ref

Async & await's past and present-51CTO.COM.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.