SoFunction
Updated on 2025-03-07

In-depth explanation of the uses in C#

Preface

Read recently.NET Threadpool starvation, and how queuing makes it worse In this blog post, I found that I had never seen a Task usage in the code in the article before. After reading some information online, I was confused and confused, which was very troubled. Today, I finally figured it out on the great day of Programmer's Day, so I wrote this essay to share with you, and I also experienced the addiction to concentrate on writing blogs.

This has never been seen before is the following codeawait ()

static async Task Process()
{
 await ();

 var tcs = new TaskCompletionSource<bool>();

 (() =>
 {
  (1000);
  (true);
 });

 ();
}

Note:The above code is not an example, it's just a first encounter because of this codeawait

Simply put, it is a task that has been completed at creation, or a task with execution time of 0, or an empty task, that is, the IsCompeted value of the Task is set to 0 at creation.

What happens to await an empty mission? We know that the current thread will be released during await. When the await task is completed, a new thread will be applied from the thread pool to continue executing the code after await. This was originally intended to solve the problem that asynchronous operations (such as IO operations) dominate the thread but cannot use the thread. However, a task that not only does not have asynchronous operations but does nothing. Isn’t it full and full?

I finally figured it out when I was having supper today - I was full and I was not full. The generated empty task is just to make wedding clothes for await, and the real plan is to use await to realize thread switching, so that the operations after await will be queued up again from the thread pool to apply for thread execution.

What are the benefits of doing this?

Threads are very, very valuable resources. It is hard to buy a thread, and it has priority. One of the important means to improve thread utilization is to allocate threads to the most needed places in a timely manner. One of the most luxurious ones is to allow an operation with low priority to execute for a long time to occupy a thread.await This allows you to cleverly use await's thread switching capabilities to place less important and time-consuming operations in a new thread (requeuing the threads applied from the thread pool). For example, many people line up to eat at grandma's house. When you came, you happened to have a seat, but you were not in a hurry and were not too hungry to eat slowly and chat slowly. Some of the people in the queue were very hungry and anxious to finish eating. At this time, if you order a few signature dishes first to satisfy your cravings, then give up your seat and queue up again, and those who line up like you do this, those who are anxious in the queue are really full of happiness, and the boss of grandma's house also bent over. The loving act of giving up your seat and queuing up again isawait ()

totalKnot

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.