SemaphoreFullException 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
对已经达到最大计数值的信号量调用 Release 方法时引发的异常。
public ref class SemaphoreFullException : Exception
public ref class SemaphoreFullException : SystemException
public class SemaphoreFullException : Exception
public class SemaphoreFullException : SystemException
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class SemaphoreFullException : SystemException
type SemaphoreFullException = class
inherit Exception
type SemaphoreFullException = class
inherit SystemException
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type SemaphoreFullException = class
inherit SystemException
Public Class SemaphoreFullException
Inherits Exception
Public Class SemaphoreFullException
Inherits SystemException
- 继承
- 继承
- 属性
示例
下面的代码示例演示一个线程中的编程错误如何导致另一个 SemaphoreFullException 线程中的 :两个线程进入信号灯。 第二个线程释放信号灯两次,而第一个线程仍在执行其任务。 当第一个线程完成并释放信号灯时,信号量计数已满,并引发异常。
#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.
*/
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.
'
注解
每次线程进入信号灯时,信号灯上的计数都会递减,并在线程释放信号量时递增。 当计数为零时,后续请求会阻止,直到其他线程释放信号灯。 当所有线程都释放信号灯时,计数为创建信号量时指定的最大值。 如果编程错误导致线程此时调用 Semaphore.Release 方法, SemaphoreFullException 则会引发 。
注意
类 Semaphore 不会在调用 WaitHandle.WaitOne 和 Semaphore.Release 方法时强制实施线程标识。 调用 的同一线程 WaitOne 不需要调用 Release。
SemaphoreFullException 不一定指示发生异常的代码存在问题。 请考虑以下情况:线程 A 和线程 B 输入最大计数为 2 的信号量。 线程 B 中的编程错误导致它调用 Release 两次,因此信号灯上的计数已满。 因此,当线程 A 最终调用 Release时, SemaphoreFullException 会引发 。
有关 SemaphoreFullException 类的实例的初始属性值列表,请参见 SemaphoreFullException() 构造函数。
构造函数
SemaphoreFullException() |
使用默认值初始化 SemaphoreFullException 类的新实例。 |
SemaphoreFullException(SerializationInfo, StreamingContext) |
用序列化数据初始化 SemaphoreFullException 类的新实例。 |
SemaphoreFullException(String) |
用指定的错误消息初始化 SemaphoreFullException 类的新实例。 |
SemaphoreFullException(String, Exception) |
使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 SemaphoreFullException 类的新实例。 |
属性
Data |
获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。 (继承自 Exception) |
HelpLink |
获取或设置指向与此异常关联的帮助文件链接。 (继承自 Exception) |
HResult |
获取或设置 HRESULT(一个分配给特定异常的编码数字值)。 (继承自 Exception) |
InnerException |
获取导致当前异常的 Exception 实例。 (继承自 Exception) |
Message |
获取描述当前异常的消息。 (继承自 Exception) |
Source |
获取或设置导致错误的应用程序或对象的名称。 (继承自 Exception) |
StackTrace |
获取调用堆栈上的即时框架字符串表示形式。 (继承自 Exception) |
TargetSite |
获取引发当前异常的方法。 (继承自 Exception) |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetBaseException() |
当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。 (继承自 Exception) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetObjectData(SerializationInfo, StreamingContext) |
当在派生类中重写时,用关于异常的信息设置 SerializationInfo。 (继承自 Exception) |
GetType() |
获取当前实例的运行时类型。 (继承自 Exception) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
创建并返回当前异常的字符串表示形式。 (继承自 Exception) |
事件
SerializeObjectState |
已过时.
当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。 (继承自 Exception) |