SemaphoreFullException クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
カウントが既に最大であるセマフォで Release メソッドが呼び出されたときにスローされる例外。
public ref class SemaphoreFullException : Exception
public ref class SemaphoreFullException : SystemException
public class SemaphoreFullException : Exception
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class SemaphoreFullException : SystemException
public class SemaphoreFullException : SystemException
type SemaphoreFullException = class
inherit Exception
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type SemaphoreFullException = class
inherit SystemException
type SemaphoreFullException = class
inherit SystemException
Public Class SemaphoreFullException
Inherits Exception
Public Class SemaphoreFullException
Inherits SystemException
- 継承
- 継承
- 属性
例
次のコード例は、あるスレッドのプログラミング エラーが別のスレッドで SemaphoreFullException につながる可能性がある方法を示しています。2 つのスレッドがセマフォに入ります。 2 番目のスレッドはセマフォを 2 回解放しますが、最初のスレッドはタスクを実行しています。 最初のスレッドが終了してセマフォを解放すると、セマフォの数は既に満杯になり、例外がスローされます。
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.
*/
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.
'
注釈
セマフォのカウントは、スレッドがセマフォに入るたびにデクリメントされ、スレッドがセマフォを解放するとインクリメントされます。 カウントが 0 の場合、後続の要求は、他のスレッドがセマフォを解放するまでブロックします。 すべてのスレッドがセマフォを解放すると、セマフォの作成時に指定された最大値がカウントされます。 プログラミング エラーにより、スレッドがこの時点で Semaphore.Release メソッドを呼び出すと、 SemaphoreFullException がスローされます。
Note
Semaphore クラスは、WaitHandle.WaitOneメソッドと Semaphore.Release メソッドの呼び出しにスレッド ID を適用しません。 WaitOneを呼び出したのと同じスレッドがReleaseを呼び出す必要はありません。
SemaphoreFullException は、例外が発生したコードに問題があることを必ずしも示すわけではありません。 次のシナリオを考えてみましょう。スレッド A とスレッド B は、最大カウントが 2 のセマフォに入ります。 スレッド B でプログラミング エラーが発生すると、 Release が 2 回呼び出され、セマフォのカウントがいっぱいになります。 その結果、スレッド A が最終的に Releaseを呼び出すと、 SemaphoreFullException がスローされます。
SemaphoreFullException クラスのインスタンスの初期プロパティ値の一覧については、SemaphoreFullException() コンストラクターを参照してください。
コンストラクター
| 名前 | 説明 |
|---|---|
| SemaphoreFullException() |
既定値を使用して、 SemaphoreFullException クラスの新しいインスタンスを初期化します。 |
| SemaphoreFullException(SerializationInfo, StreamingContext) |
シリアル化されたデータを使用して、 SemaphoreFullException クラスの新しいインスタンスを初期化します。 |
| SemaphoreFullException(String, Exception) |
指定したエラー メッセージと、この例外の原因である内部例外への参照を使用して、 SemaphoreFullException クラスの新しいインスタンスを初期化します。 |
| SemaphoreFullException(String) |
指定したエラー メッセージを使用して、 SemaphoreFullException クラスの新しいインスタンスを初期化します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| Data |
例外に関する追加のユーザー定義情報を提供するキーと値のペアのコレクションを取得します。 (継承元 Exception) |
| HelpLink |
この例外に関連付けられているヘルプ ファイルへのリンクを取得または設定します。 (継承元 Exception) |
| HResult |
特定の例外に割り当てられるコード化された数値である HRESULT を取得または設定します。 (継承元 Exception) |
| InnerException |
現在の例外の原因となった Exception インスタンスを取得します。 (継承元 Exception) |
| Message |
現在の例外を説明するメッセージを取得します。 (継承元 Exception) |
| Source |
エラーの原因となるアプリケーションまたはオブジェクトの名前を取得または設定します。 (継承元 Exception) |
| StackTrace |
呼び出し履歴のイミディエイト フレームの文字列表現を取得します。 (継承元 Exception) |
| TargetSite |
現在の例外をスローするメソッドを取得します。 (継承元 Exception) |
メソッド
| 名前 | 説明 |
|---|---|
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| GetBaseException() |
派生クラスでオーバーライドされた場合、1 つ以上の後続の例外の根本原因である Exception を返します。 (継承元 Exception) |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
派生クラスでオーバーライドされた場合は、例外に関する情報を使用して SerializationInfo を設定します。 (継承元 Exception) |
| GetType() |
現在のインスタンスのランタイム型を取得します。 (継承元 Exception) |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| ToString() |
現在の例外の文字列形式を作成して返します。 (継承元 Exception) |
イベント
| 名前 | 説明 |
|---|---|
| SerializeObjectState |
例外に関するシリアル化されたデータを含む例外状態オブジェクトを作成するために例外がシリアル化されるときに発生します。 (継承元 Exception) |