SemaphoreFullException クラス
アセンブリ: System (system.dll 内)

<SerializableAttribute> _ <ComVisibleAttribute(False)> _ Public Class SemaphoreFullException Inherits SystemException
[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.Exception
System.SystemException
System.Threading.SemaphoreFullException


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- SemaphoreFullException クラスのページへのリンク