SemaphoreFullException クラスとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > SemaphoreFullException クラスの意味・解説 

SemaphoreFullException クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

カウントが既に最大値であるセマフォに対して System.Threading.Semaphore.Release メソッド呼び出され場合スローされる例外

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

<SerializableAttribute> _
<ComVisibleAttribute(False)> _
Public Class SemaphoreFullException
    Inherits SystemException
Dim instance As SemaphoreFullException
[SerializableAttribute] 
[ComVisibleAttribute(false)] 
public class SemaphoreFullException : SystemException
[SerializableAttribute] 
[ComVisibleAttribute(false)] 
public ref class SemaphoreFullException : public
 SystemException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(false) */ 
public class SemaphoreFullException extends
 SystemException
SerializableAttribute 
ComVisibleAttribute(false) 
public class SemaphoreFullException extends
 SystemException
解説解説

セマフォカウントは、1 つスレッドセマフォに入るたびにデクリメントされ、1 つスレッドセマフォ解放するたびにインクリメントされますカウントが 0 になると、それ以降要求は他のスレッドセマフォ解放するまでブロックされます。すべてのスレッドセマフォ解放すると、カウントセマフォ作成時に設定され最大値なりますプログラミング エラーによってこの時点スレッドSystem.Threading.Semaphore.Release メソッド呼び出した場合SemaphoreFullExceptionスローさます。

メモメモ

Semaphore クラスは、System.Threading.WaitHandle.WaitOne および System.Threading.Semaphore.Release メソッド呼び出しスレッドID適用しません。WaitOne呼び出したスレッドRelease呼び出すメソッドが同じである必要はありません。

SemaphoreFullException は、この例外発生したコード問題があることを示すとは限りません。たとえば、スレッド A とスレッド B が、カウント最大値が 2 のセマフォ入ったとしますスレッド B がプログラミング エラーによって Release を 2 回呼び出した場合セマフォカウント最大値なりますその結果スレッド A が最終的に Release呼び出すと、SemaphoreFullExceptionスローさます。

SemaphoreFullException クラスインスタンス初期プロパティ値の一覧については、SemaphoreFullException コンストラクタトピック参照してください

使用例使用例

1 つスレッドプログラミング エラーがあるために、別のスレッドに対して SemaphoreFullExceptionスローされるコード例次に示します。この例では、2 つスレッドセマフォ入ります。1 番目のスレッドタスク実行している間に、2 番目のスレッドセマフォを 2 回解放します。1 番目のスレッドタスク完了してセマフォ解放しようとすると、セマフォカウントが既に最大値になっているために、例外スローさます。

Imports System
Imports System.Threading

Public Class Example

    ' A semaphore that can satisfy at most two concurrent
    ' requests.
    '
    Private Shared _pool As
 New Semaphore(2, 2)

    <MTAThread> _
    Public Shared Sub Main()
        ' Create and start two threads, A and B. 
        '
        Dim tA As New Thread(AddressOf
 ThreadA)
        tA.Start()

        Dim tB As New Thread(AddressOf
 ThreadB)
        tB.Start()

    End Sub

    Private Shared Sub ThreadA()
        ' Thread A enters the semaphore and simulates a task
        ' that lasts a second.
        '
        _pool.WaitOne()
        Console.WriteLine("Thread A entered the semaphore.")

        Thread.Sleep(1000)

        Try
            _pool.Release()
            Console.WriteLine("Thread A released the semaphore.")
        Catch ex As Exception
            Console.WriteLine("Thread A: {0}", ex.Message)
        End Try
    End Sub

    Private Shared Sub ThreadB()
        ' Thread B simulates a task that lasts half a second,
        ' then enters the semaphore.
        '
        Thread.Sleep(500)

        _pool.WaitOne()
        Console.WriteLine("Thread B entered the semaphore.")
        
        ' Due to a programming error, Thread B releases the
        ' semaphore twice. To fix the program, delete one line.
        _pool.Release()
        _pool.Release()
        Console.WriteLine("Thread B exits successfully.")
    End Sub
End Class
' This code example produces the following output:
'
' Thread A entered the semaphore.
' Thread B entered the semaphore.
' Thread B exits successfully.
' Thread A: Adding the given count to the semaphore would cause it to
 exceed its maximum count.
'
using System;
using System.Threading;

public class Example
{
    // A semaphore that can satisfy at most two concurrent
    // requests.
    //
    private static Semaphore _pool = new
 Semaphore(2, 2);

    public static void Main()
    {
        // Create and start two threads, A and B. 
        //
        Thread tA = new Thread(new ThreadStart(ThreadA));
        tA.Start();

        Thread tB = new Thread(new ThreadStart(ThreadB));
        tB.Start();
    }

    private static void
 ThreadA()
    {
        // Thread A enters the semaphore and simulates a task
        // that lasts a second.
        //
        _pool.WaitOne();
        Console.WriteLine("Thread A entered the semaphore.");

        Thread.Sleep(1000);

        try
        {
            _pool.Release();
            Console.WriteLine("Thread A released the semaphore.");
        }
        catch(Exception ex)
        {
            Console.WriteLine("Thread A: {0}", ex.Message);
        }
    }

    private static void
 ThreadB()
    {
        // Thread B simulates a task that lasts half a second,
        // then enters the semaphore.
        //
        Thread.Sleep(500);

        _pool.WaitOne();
        Console.WriteLine("Thread B entered the semaphore.");
        
        // Due to a programming error, Thread B releases the
        // semaphore twice. To fix the program, delete one line.
        _pool.Release();
        _pool.Release();
        Console.WriteLine("Thread B exits successfully.");
    }
}
/* This code example produces the following output:

Thread A entered the semaphore.
Thread B entered the semaphore.
Thread B exits successfully.
Thread A: Adding the given count to the semaphore would cause it to exceed its maximum
 count.
 */
#using <System.dll>
using namespace System;
using namespace System::Threading;

public ref class Example
{
private:
   // A semaphore that can satisfy at most two concurrent
   // requests.
   //
   static Semaphore^ _pool = gcnew Semaphore( 2,2 );

public:
   static void main()
   {
      // Create and start two threads, A and B.
      //
      Thread^ tA = gcnew Thread( gcnew ThreadStart( ThreadA ) );
      tA->Start();

      Thread^ tB = gcnew Thread( gcnew ThreadStart( ThreadB ) );
      tB->Start();
   }

private:
   static void ThreadA()
   {
      // Thread A enters the semaphore and simulates a task
      // that lasts a second.
      //
      _pool->WaitOne();
      Console::WriteLine( L"Thread A entered the semaphore." );

      Thread::Sleep( 1000 );

      try
      {
         _pool->Release();
         Console::WriteLine( L"Thread A released the semaphore." );
      }
      catch ( Exception^ ex ) 
      {
         Console::WriteLine( L"Thread A: {0}", ex->Message );
      }
   }

   static void ThreadB()
   {
      // Thread B simulates a task that lasts half a second,
      // then enters the semaphore.
      //
      Thread::Sleep( 500 );

      _pool->WaitOne();
      Console::WriteLine( L"Thread B entered the semaphore." );
      
      // Due to a programming error, Thread B releases the
      // semaphore twice. To fix the program, delete one line.
      _pool->Release();
      _pool->Release();
      Console::WriteLine( L"Thread B exits successfully." );
   }
};
/* This code example produces the following output:

Thread A entered the semaphore.
Thread B entered the semaphore.
Thread B exits successfully.
Thread A: Adding the given count to the semaphore would cause it to exceed its maximum
 count.
 */
import System.*;
import System.Threading.*;

public class Example
{
    // A semaphore that can satisfy at most two concurrent
    // requests.
    //
    private static Semaphore pool = new
 Semaphore(2, 2);

    public static void main(String[]
 args)
    {
        // Create and start two threads, A and B. 
        //
        System.Threading.Thread tA = new System.Threading.Thread(new
 
            ThreadStart(ThreadA));
        tA.Start();

        System.Threading.Thread tB = new System.Threading.Thread(new
 
            ThreadStart(ThreadB));
        tB.Start();
    } //main

    private static void
 ThreadA()
    {
        // Thread A enters the semaphore and simulates a task
        // that lasts a second.
        //
        pool.WaitOne();
        Console.WriteLine("Thread A entered the semaphore.");
        System.Threading.Thread.Sleep(1000);

        try {
            pool.Release();
            Console.WriteLine("Thread A released the semaphore.");
        }
        catch (System.Exception ex) {
            Console.WriteLine("Thread A: {0}", ex.get_Message());
        }
    } //ThreadA

    private static void
 ThreadB()
    {
        // Thread B simulates a task that lasts half a second,
        // then enters the semaphore.
        //
        System.Threading.Thread.Sleep(500);
        pool.WaitOne();
        Console.WriteLine("Thread B entered the semaphore.");
        // Due to a programming error, Thread B releases the
        // semaphore twice. To fix the program, delete one line.
        pool.Release();
        pool.Release();
        Console.WriteLine("Thread B exits successfully.");
    } //ThreadB
} //Example
/* This code example produces the following output:

Thread A entered the semaphore.
Thread B entered the semaphore.
Thread B exits successfully.
Thread A: Adding the given count to the semaphore would cause it to 
exceed its maximum count.
 */
継承階層継承階層
System.Object
   System.Exception
     System.SystemException
      System.Threading.SemaphoreFullException
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



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

辞書ショートカット

すべての辞書の索引

「SemaphoreFullException クラス」の関連用語

SemaphoreFullException クラスのお隣キーワード
検索ランキング

   

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



SemaphoreFullException クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS