视频下载VS Monitor.Wait VS RegisteredWaitHandle? [英] Thread.sleep vs Monitor.Wait vs RegisteredWaitHandle?

查看:199
本文介绍了视频下载VS Monitor.Wait VS RegisteredWaitHandle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(以下物品都有不同的目标,但即时通讯有趣知道他们是如何暂停)

问题

视频下载 - ?它是否影响性能的系统上它占用一个线程,其等待

Thread.sleep - Does it impact performance on a system ?does it tie up a thread with its wait ?

怎么样 Monitor.Wait ?什么是他们等待的方式有什么区别? ?他们阻碍线程他们等待?

what about Monitor.Wait ? what is the difference in the way they "wait"? do they tie up a thread with their wait ?

怎么样 RegisteredWaitHandle ?该方法接受时执行等待委托 手柄信号。虽然它的等待,它没有占用一个线程。

what about RegisteredWaitHandle ? This method accepts a delegate that is executed when a wait handle is signaled. While it’s waiting, it doesn’t tie up a thread.

所以一些线程被暂停,可以通过委托被唤醒,而其他人只是等待?旋转?

so some thread are paused and can be woken by a delegate , while others just wait ? spin ?

有人可以让事情更清晰?

can someone please make things clearer ?

修改

http://www.albahari.com/threading/part2.aspx

推荐答案

两个 Thread.sleep代码 Monitor.Wait 把线程 WaitSleepJoin 国家

Both Thread.Sleep and Monitor.Wait put the thread in the WaitSleepJoin state:

WaitSleepJoin:线程被阻塞。这可能是调用的结果   主题::睡眠或主题::加入,请求锁的 - 例如,   调用监控::输入或显示器::等待 - 等待的线程或   同步对象,如ManualResetEvent的。

WaitSleepJoin: The thread is blocked. This could be the result of calling Thread::Sleep or Thread::Join, of requesting a lock — for example, by calling Monitor::Enter or Monitor::Wait — or of waiting on a thread synchronization object such as ManualResetEvent.

RegisteredWaitHandle 通过调用获得<一href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.registerwaitforsingleobject.aspx">RegisterWaitForSingleObject并通过一个的WaitHandle 。一般这个类使用的所有后代阻塞机制,因此调用等待将再次把线程 WaitSleepJoin (如的AutoResetEvent )。

RegisteredWaitHandle is obtained by calling RegisterWaitForSingleObject and passing a WaitHandle. Generally all descendants of this class use blocking mechanisms, so calling Wait will again put the thread in WaitSleepJoin (e.g. AutoResetEvent).

下面是另一个报价从MSDN:

Here's another quote from MSDN:

该RegisterWaitForSingleObject方法检查的当前状态   指定对象的WaitHandle。如果对象的状态是unsignaled,   该方法注册一个等待操作。执行等待操作   从线程池中的线程。委托是由工人执行   当线程对象的状态变为信号,或者超时   间隔已过期。

The RegisterWaitForSingleObject method checks the current state of the specified object's WaitHandle. If the object's state is unsignaled, the method registers a wait operation. The wait operation is performed by a thread from the thread pool. The delegate is executed by a worker thread when the object's state becomes signaled or the time-out interval elapses.

因此​​,在池中的线​​程的并等待作为信号。

So a thread in the pool does wait for the signal.

这篇关于视频下载VS Monitor.Wait VS RegisteredWaitHandle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆