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.

注解

仅当前锁的所有者可以发出信号等待对象使用PulseOnly 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释放锁后,在准备就绪的队列 (它不一定接收到脉冲的线程) 中的下一个线程获取该锁。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.

重要

Monitor类不维护状态,指示Pulse调用方法。The 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. 如果使用的两个线程PulseWait进行交互,这可能导致死锁。If two threads are using Pulse and Wait to interact, this could result in a deadlock. 与此相反的行为AutoResetEvent类:如果你发出信号AutoResetEvent通过调用其Set方法中,没有线程在等待,并且AutoResetEvent保持在终止状态,直到某个线程调用WaitOneWaitAny,或WaitAllContrast 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. AutoResetEvent释放该线程并返回到未终止的状态。The 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.

PulsePulseAll,和Wait方法必须从调用代码的同步块内。The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

若要通知的多个线程信息,请使用PulseAll方法。To signal multiple threads, use the PulseAll method.

适用于

另请参阅