简体   繁体   English

Task.Delay是否无阻塞?

[英]Is Task.Delay non blocking?

Task.Delay uses System.Threading.Timer internally. Task.Delay内部使用System.Threading.Timer Main question is System.Threading.Timer non-blocking for my application? 主要问题是System.Threading.Timer非阻塞我的应用程序? If I make: 如果我做:

await Task.Delay(15)

Does code bellow utilize threads for 15ms? 代码是否会使用线程15ms? Or will it use OS to make callback. 或者它会使用操作系统进行回调。 Where to find information about this? 哪里可以找到相关信息?

According to Jeffrey Richter (page 748): 根据杰弗里里希特(第748页)的说法:

Internally, the thread pool has just one thread that it uses for all Timer objects. 在内部,线程池只有一个线程用于所有Timer对象。 This thread knows when the next Timer object's time is due. 该线程知道下一个Timer对象的时间到期。 When the next Timer object is due, the thread wakes up, and internally calls ThreadPool's QueueUserWorkItem to enter an entry into the thread pool's queue, causing your callback method to get called. 当下一个Timer对象到期时,线程唤醒,并在内部调用ThreadPool的QueueUserWorkItem以输入线程池队列中的条目,从而导致调用回调方法。

That means that you'll have at least two worker threads when timer is executed: one - internal for timer and another - for the actual worker process. 这意味着在执行计时器时,您将至少有两个工作线程:一个 - 内部用于计时器,另一个用于实际工作进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM