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.

注解

只有锁定的当前所有者才能使用发送正在等待的对象 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 释放锁定,则就绪队列中的下一个线程 (这不一定是 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.

重要

Monitor类不维护指示已调用方法的状态 PulseThe 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 一直保持终止状态,直到线程调用 WaitOneWaitAnyWaitAllContrast 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.

Pulse PulseAll 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.

适用于

另请参阅