Monitor.Pulse(Object) 方法

定义

通知等待队列中的线程锁定对象状态的更改。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

线程正在等待的对象。The object a thread is waiting for.

异常

obj 参数为 nullThe obj parameter is null.

调用线程不拥有指定对象的锁。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线程调用WaitOneWaitAnyWaitAll之前,将一直保持终止状态。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.

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

若要向多个线程发出PulseAll信号,请使用方法。To signal multiple threads, use the PulseAll method.

适用于

另请参阅