Monitor.Pulse メソッド
アセンブリ: mscorlib (mscorlib.dll 内)



Pulse を使用して待機中のオブジェクトに通知できるのは、ロックの現在の所有者だけです。
指定したオブジェクトのロックを現在所有しているスレッドは、このメソッドを呼び出して、ロックの取得を待機している次のスレッドに通知します。待機中のスレッドは、パルスを受信すると実行待ちキューへ移動します。Pulse を呼び出したスレッドがロックを解放すると、実行待ちキュー内の次のスレッドがロックを取得します。このスレッドは、必ずしもパルスを受け取ったスレッドになるとは限りません。
![]() |
---|
Monitor クラスは、Pulse メソッドが呼び出されたことを示す状態を維持しません。したがって、待機中のスレッドがないときに Pulse を呼び出すと、Wait を呼び出す次のスレッドによってブロックされ、Pulse が呼び出されていない場合と同じ状態になります。2 つのスレッドが Pulse および Wait を使用すると、デッドロック状態が発生する場合があります。AutoResetEvent クラスの動作と比較してみましょう。AutoResetEvent を Set メソッドの呼び出しによって通知したときに待機中のスレッドがない場合は、WaitOne、WaitAny、または WaitAll がスレッドに呼び出されない限り、AutoResetEvent はシグナル状態のままです。AutoResetEvent がそのスレッドを解放すると、非シグナル状態に戻ります。 |
同期されたオブジェクトは、現在ロックを保持しているスレッドへの参照、ロックを取得する準備ができているスレッドを格納している実行待ちキューへの参照、オブジェクトの状態の変更通知を待機しているスレッドを格納している待機キューへの参照など、複数の参照を保持しています。

Pulse メソッドを使用する方法を次のコード例に示します。
Imports System Imports System.Threading Imports System.Collections Namespace MonitorCS1 Class MonitorSample Private MAX_LOOP_TIME As Integer = 1000 Private m_smplQueue As Queue Public Sub New() m_smplQueue = New Queue() End Sub 'New Public Sub FirstThread() Dim counter As Integer = 0 SyncLock m_smplQueue While counter < MAX_LOOP_TIME 'Wait, if the queue is busy. Monitor.Wait(m_smplQueue) 'Push one element. m_smplQueue.Enqueue(counter) 'Release the waiting thread. Monitor.Pulse(m_smplQueue) counter += 1 End While End SyncLock End Sub 'FirstThread Public Sub SecondThread() SyncLock m_smplQueue 'Release the waiting thread. Monitor.Pulse(m_smplQueue) 'Wait in the loop while the queue is busy. 'Exit on the time-out when the first thread stops. While Monitor.Wait(m_smplQueue, 1000) 'Pop the first element. Dim counter As Integer = CInt(m_smplQueue.Dequeue()) 'Print the first element. Console.WriteLine(counter.ToString()) 'Release the waiting thread. Monitor.Pulse(m_smplQueue) End While End SyncLock End Sub 'SecondThread 'Return the number of queue elements. Public Function GetQueueCount() As Integer Return m_smplQueue.Count End Function 'GetQueueCount Public Shared Sub Main(args() As String) 'Create the MonitorSample object. Dim test As New MonitorSample() 'Create the first thread. Dim tFirst As New Thread(AddressOf test.FirstThread) 'Create the second thread. Dim tSecond As New Thread(AddressOf test.SecondThread) 'Start threads. tFirst.Start() tSecond.Start() 'wait to the end of the two threads tFirst.Join() tSecond.Join() 'Print the number of queue elements. Console.WriteLine(("Queue Count = " + test.GetQueueCount().ToString())) End Sub 'Main End Class 'MonitorSample End Namespace 'MonitorCS1
using System; using System.Threading; using System.Collections; namespace MonitorCS1 { class MonitorSample { const int MAX_LOOP_TIME = 1000; Queue m_smplQueue; public MonitorSample() { m_smplQueue = new Queue(); } public void FirstThread() { int counter = 0; lock(m_smplQueue) { while(counter < MAX_LOOP_TIME) { //Wait, if the queue is busy. Monitor.Wait(m_smplQueue); //Push one element. m_smplQueue.Enqueue(counter); //Release the waiting thread. Monitor.Pulse(m_smplQueue); counter++; } } } public void SecondThread() { lock(m_smplQueue) { //Release the waiting thread. Monitor.Pulse(m_smplQueue); //Wait in the loop, while the queue is busy. //Exit on the time-out when the first thread stops. while(Monitor.Wait(m_smplQueue,1000)) { //Pop the first element. int counter = (int)m_smplQueue.Dequeue(); //Print the first element. Console.WriteLine(counter.ToString()); //Release the waiting thread. Monitor.Pulse(m_smplQueue); } } } //Return the number of queue elements. public int GetQueueCount() { return m_smplQueue.Count; } static void Main(string[] args) { //Create the MonitorSample object. MonitorSample test = new MonitorSample(); //Create the first thread. Thread tFirst = new Thread(new ThreadStart(test.FirstThread)); //Create the second thread. Thread tSecond = new Thread(new ThreadStart(test.SecondThread)); //Start threads. tFirst.Start(); tSecond.Start(); //wait to the end of the two threads tFirst.Join(); tSecond.Join(); //Print the number of queue elements. Console.WriteLine("Queue Count = " + test.GetQueueCount().ToString()); } } }
using namespace System; using namespace System::Threading; using namespace System::Collections; ref class MonitorSample { public: MonitorSample(); void FirstThread(); void SecondThread(); int GetQueueCount(); private: literal int MAX_LOOP_TIME = 1000; Queue^ m_smplQueue; }; MonitorSample::MonitorSample() { m_smplQueue = gcnew Queue; } void MonitorSample::FirstThread() { int counter = 0; Monitor::Enter( m_smplQueue ); while ( counter < MAX_LOOP_TIME ) { //Wait, if the queue is busy. Monitor::Wait( m_smplQueue ); //Push one element. m_smplQueue->Enqueue( counter ); //Release the waiting thread. Monitor::Pulse( m_smplQueue ); counter++; } Monitor::Exit( m_smplQueue ); } void MonitorSample::SecondThread() { Monitor::Enter( m_smplQueue ); //Release the waiting thread. Monitor::Pulse( m_smplQueue ); //Wait in the loop, while the queue is busy. //Exit on the time-out when the first thread stopped. while ( Monitor::Wait( m_smplQueue, 1000 ) ) { //Pop the first element. int counter = *dynamic_cast<int^>(m_smplQueue->Dequeue()); //Print the first element. Console::WriteLine( counter.ToString() ); //Release the waiting thread. Monitor::Pulse( m_smplQueue ); } Monitor::Exit( m_smplQueue ); } //Return the number of queue elements. int MonitorSample::GetQueueCount() { return m_smplQueue->Count; } int main() { //Create the MonitorSample object. MonitorSample^ test = gcnew MonitorSample; //Create the first thread. Thread^ tFirst = gcnew Thread( gcnew ThreadStart( test, &MonitorSample::FirstThread ) ); //Create the second thread. Thread^ tSecond = gcnew Thread( gcnew ThreadStart( test, &MonitorSample::SecondThread ) ); //Start threads. tFirst->Start(); tSecond->Start(); //wait to the end of the two threads tFirst->Join(); tSecond->Join(); //Print the number of queue elements. Console::WriteLine( String::Concat( "Queue Count = ", test->GetQueueCount().ToString() ) ); }
package MonitorJSL1; import System.*; import System.Threading.*; import System.Collections.*; class MonitorSample { private int MAX_LOOP_TIME = 1000; private Queue mSmplQueue; public MonitorSample() { mSmplQueue = new Queue(); } //MonitorSample public void FirstThread() { int counter = 0; synchronized (mSmplQueue) { while (counter < MAX_LOOP_TIME) { //Wait, if the queue is busy. Monitor.Wait(mSmplQueue); //Push one element. mSmplQueue.Enqueue((Int32)counter); //Release the waiting thread. Monitor.Pulse(mSmplQueue); counter++; } } } //FirstThread public void SecondThread() { synchronized (mSmplQueue) { //Release the waiting thread. Monitor.Pulse(mSmplQueue); //Wait in the loop, while the queue is busy. //Exit on the time-out when the first thread stops. while (Monitor.Wait(mSmplQueue, 1000)) { //Pop the first element. int counter = Convert.ToInt32(mSmplQueue.Dequeue()); //Print the first element. Console.WriteLine(Convert.ToString(counter)); //Release the waiting thread. Monitor.Pulse(mSmplQueue); } } } //SecondThread //Return the number of queue elements. public int GetQueueCount() { return mSmplQueue.get_Count(); } //GetQueueCount public static void main(String[] args) { //Create the MonitorSample object. MonitorSample test = new MonitorSample(); //Create the first thread. System.Threading.Thread tFirst = new System.Threading.Thread(new ThreadStart(test.FirstThread)); //Create the second thread. System.Threading.Thread tSecond = new System.Threading.Thread(new ThreadStart(test.SecondThread)); //Start threads. tFirst.Start(); tSecond.Start(); //wait to the end of the two threads tFirst.Join(); tSecond.Join(); //Print the number of queue elements. Console.WriteLine("Queue Count = " + Convert.ToString(test.GetQueueCount())); } //main } //MonitorSample

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からMonitor.Pulse メソッドを検索する場合は、下記のリンクをクリックしてください。

- Monitor.Pulse メソッドのページへのリンク