AutoResetEvent.WaitOne() 是否释放线程池中的槽?
我正在尝试同步异步方法。异步版本的主要优点是它释放了线程池中的一个槽。我想在我的同步版本中保留这个优势。当我使用 AutoResetEvent.WaitOne() 时,就线程池使用而言,它相当于 Thread.Sleep() 吗?
I am trying to synchronize an asynchronous method. The main advantage of the async version is that it frees a slot in the thread pool. I would like to keep this advantage in my sync version. When I use AutoResetEvent.WaitOne() it is equivalent to a Thread.Sleep() in terms of thread pool usage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用
WaitOne
时,当前线程将阻塞并等待事件发出信号。就像 Thread.Sleep 一样,线程不会被释放到线程池中。不同之处在于,使用 Thread.Sleep 时,您需要指定一个固定时间,在此期间当前线程将被阻塞,而WaitOne
将阻塞,直到其他线程调用设置
否则会发生超时。When you call
WaitOne
the current thread will block and wait for the event to be signaled. Just like withThread.Sleep
the thread will not be released to the thread pool. The difference is that withThread.Sleep
you need to specify a fixed time during which the current thread will be blocked, whileWaitOne
will block until some other thread callsSet
or a timeout occurs.