Monitor.Pulse メソッドとは? わかりやすく解説

Monitor.Pulse メソッド

ロックされオブジェクトの状態が変更されたことを、待機キュー内のスレッド通知します

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentNullException

obj パラメータnull 参照 (Visual Basic では Nothing) です。

SynchronizationLockException

呼び出し元のスレッドは、指定したオブジェクトロック所有していません。

解説解説

Pulse使用して待機中のオブジェクト通知できるのは、ロック現在の所有者だけです。

指定したオブジェクトロックを現在所有しているスレッドは、このメソッド呼び出してロック取得待機している次のスレッド通知します待機中のスレッドは、パルス受信する実行待ちキュー移動しますPulse呼び出したスレッドロック解放すると、実行待ちキュー内の次のスレッドロック取得します。このスレッドは、必ずしもパルス受け取ったスレッドになるとは限りません。

メモ重要 :

Monitor クラスは、Pulse メソッド呼び出されたことを示す状態を維持しません。したがって待機中のスレッドがないときに Pulse呼び出すと、Wait呼び出す次のスレッドによってブロックされPulse呼び出されていない場合と同じ状態になります2 つスレッドPulse および Wait使用すると、デッドロック状態が発生する場合あります。AutoResetEvent クラス動作比較してましょうAutoResetEventSet メソッド呼び出しによって通知したときに待機中のスレッドない場合は、WaitOne、WaitAny、または WaitAll がスレッド呼び出されない限りAutoResetEventシグナル状態のままです。AutoResetEvent がそのスレッド解放すると、非シグナル状態に戻ります

同期されたオブジェクトは、現在ロック保持しているスレッドへの参照ロック取得する準備ができているスレッド格納している実行待ちキューへの参照オブジェクトの状態の変更通知待機しているスレッド格納している待機キューへの参照など、複数参照保持してます。

Pulse、PulseAll、Wait の各メソッドは、同期されたコードブロック内から呼び出される必要があります

複数スレッド通知するには、PulseAll メソッド使用します

使用例使用例

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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からMonitor.Pulse メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からMonitor.Pulse メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からMonitor.Pulse メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Monitor.Pulse メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Monitor.Pulse メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS