Monitor.Pulse(Object) Monitor.Pulse(Object) Monitor.Pulse(Object) Monitor.Pulse(Object) Method

定義

通知等候佇列中的執行緒,鎖定物件的狀態有所變更。Notifies a thread in the waiting queue of a change in the locked object's state.

public:
 static void Pulse(System::Object ^ obj);
public static void Pulse (object obj);
static member Pulse : obj -> unit
Public Shared Sub Pulse (obj As Object)

參數

obj
Object Object Object Object

執行緒正等候的物件。The object a thread is waiting for.

例外狀況

呼叫執行緒沒有指定物件的鎖定。The calling thread does not own the lock for the specified object.

備註

只有目前的鎖定擁有者可以使用Pulse通知等候中的物件。Only the current owner of the lock can signal a waiting object using Pulse.

目前擁有指定物件之鎖定的執行緒會叫用這個方法,以通知行中的下一個執行緒鎖定。The thread that currently owns the lock on the specified object invokes this method to signal the next thread in line for the lock. 收到脈衝之後,等候中的執行緒就會移至 [就緒] 佇列。Upon receiving the pulse, the waiting thread is moved to the ready queue. 當叫用的執行緒Pulse釋放鎖定時,就緒佇列中的下一個執行緒(不一定是 pulsed 的執行緒)會取得鎖定。When the thread that invoked Pulse releases the lock, the next thread in the ready queue (which is not necessarily the thread that was pulsed) acquires the lock.

重要

類別不會維護狀態,表示Pulse已呼叫方法。 MonitorThe Monitor class does not maintain state indicating that the Pulse method has been called. 因此,如果您在Pulse沒有任何執行緒等候時呼叫,則呼叫Wait的下一個執行緒會Pulse如同從未呼叫過。Thus, if you call Pulse when no threads are waiting, the next thread that calls Wait blocks as if Pulse had never been called. 如果有兩個執行緒Pulse正在Wait使用並進行互動,這可能會導致鎖死。If two threads are using Pulse and Wait to interact, this could result in a deadlock. 這與AutoResetEvent類別的行為相反:如果您AutoResetEvent藉由呼叫其Set方法來發出信號,而且沒有等待的執行緒, AutoResetEvent則會維持在已發出信號的狀態WaitOne,直到執行緒WaitAll呼叫、 WaitAny或為止。Contrast this with the behavior of the AutoResetEvent class: If you signal an AutoResetEvent by calling its Set method, and there are no threads waiting, the AutoResetEvent remains in a signaled state until a thread calls WaitOne, WaitAny, or WaitAll. 執行緒和會回到未收到訊號狀態的版本。AutoResetEventThe AutoResetEvent releases that thread and returns to the unsignaled state.

請注意,已同步處理的物件會保留數個參考,包括目前持有鎖定之執行緒的參考、就緒佇列的參考,其中包含準備好取得鎖定的執行緒,以及等候佇列的參考。包含等候物件狀態變更通知的執行緒。Note that a synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state.

Pulse、和方法Wait必須從已同步處理的程式碼區塊中叫用。 PulseAllThe Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

若要發出多個執行緒的PulseAll信號,請使用方法。To signal multiple threads, use the PulseAll method.

適用於

另請參閱