Asynchronous and thread blocking, asynchronous thread Blocking

Source: Internet
Author: User

Asynchronous and thread blocking, asynchronous thread Blocking

Returned directory

It should be understood in this way

Asynchronous: early developers had many misunderstandings about it. They thought that the main thread without blocking is asynchronous, And the UI without blocking is asynchronous. However, Asynchronization is not closely related to these two things, the emergence of Asynchronization mainly aims to improve the utilization of threads and make available threads higher, instead of doing only one thing for a thread. If this is not completed, the following things will not be done. This is incorrect, the thread should be freed! In fact, if you have learned nodejs, it should be clearer about non-blocking of a single thread. It mainly implements asynchronous callback through method callback, but the syntax is not the same as that of C.

Let's talk about the misunderstanding mentioned above.

Misunderstanding 1: Do not block the main thread

If the main thread is not blocked, you can only open a new thread to complete this action. Like some system notifications, it has nothing to do with the main thread's workflow. If you open a new thread, it is executed in parallel with the main thread. This is not asynchronous. It is only multi-thread! It will increase thread spending. Improper use will affect the system throughput!

Misunderstanding 2: Do not block the UI

This is even more nonsense. For a workflow, it must be executed in the order of 1, 2, and 3. For Synchronous Code, it is executed by a thread from 1 to 3, this thread will be occupied all the time! For Asynchronous code, when it is executed to 1, the thread is recycled to the pool, which can be used by others. After 1 is executed, a new thread is taken from the thread pool to continue execution, this is called asynchronous! C # is asynchronous and friendly. With async, await can be implemented!

Experiment: view the number of valid threads remaining

// <Summary> // The thread is not blocked, high Thread utilization // After the await thread is used, you can do other things // After the await method ends, apply for a new thread to execute the following code // </summary> // /<returns> </returns> [Route ("~ /Do5 ")] public async Task <string> Do5 () {var sw = new Stopwatch (); sw. start (); await HttpHelper. get ("http: // localhost: 61699/do1"); await HttpHelper. get ("http: // localhost: 61699/do2"); await HttpHelper. get ("http: // localhost: 61699/do3"); await HttpHelper. get ("http: // localhost: 61699/do4"); sw. stop (); int workerThreads, completionPortThreads; ThreadPool. getAvailableThreads (out workerThreads, out completionPortThreads); return $ "max threads: {workerThreads}, completionPortThreads: {completionPortThreads}, timer: {sw. elapsedMilliseconds. toString ()}";}

Let's take a look at its I/O thread surplus. It refresh several times and remains between 32766 and 32765.

If you use the synchronization code, the result will be different. You can refer to the following for the rest of the thread:

The thread ID is different in each await

The following is a more obvious test. Execute multiple await tasks in sequence and obtain the ID of the current thread. They may be different in an asynchronous environment, because every time you get a new thread from the pool!

            await HttpHelper.Get("http://localhost:61699/do1");            str.Append($"step1:{Thread.CurrentThread.ManagedThreadId}");            await HttpHelper.Get("http://localhost:61699/do2");            str.Append($"step2:{Thread.CurrentThread.ManagedThreadId}");            await HttpHelper.Get("http://localhost:61699/do3");            str.Append($"step3:{Thread.CurrentThread.ManagedThreadId}");            await HttpHelper.Get("http://localhost:61699/do4");            str.Append($"step4:{Thread.CurrentThread.ManagedThreadId}");

Through this article, we should really understand the concept of Asynchronization. Remember, Asynchronization is mainly used to improve thread utilization and system throughput. It is similar to parallelism, the main thread blocking has a direct relationship and is not the focus of its research. Please remember this!

Returned directory

Related Article

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.