SemaphoreFullException 类

定义

对已经达到最大计数值的信号量调用 Release 方法时引发的异常。The exception that is thrown when the Release method is called on a semaphore whose count is already at the maximum.

public ref class SemaphoreFullException : SystemException
public class SemaphoreFullException : SystemException
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class SemaphoreFullException : SystemException
type SemaphoreFullException = class
    inherit SystemException
Public Class SemaphoreFullException
Inherits SystemException
继承
SemaphoreFullException
继承
SemaphoreFullException
属性

示例

下面的代码示例演示一个线程中的编程错误如何在另一个线程中导致 SemaphoreFullException:两个线程输入一个信号量。The following code example shows how a programming error in one thread can lead to a SemaphoreFullException in another thread: Two threads enter a semaphore. 第二个线程两次释放信号量,而第一个线程仍在执行其任务。The second thread releases the semaphore twice, while the first thread is still executing its task. 当第一个线程完成并释放信号量后,信号量计数已满,并引发异常。When the first thread finishes and releases the semaphore, the semaphore count is already full and an exception is thrown.

#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.
'

注解

每次线程进入信号量时,信号量的计数都将减少,并在线程释放信号量时递增。The count on a semaphore is decremented each time a thread enters the semaphore, and incremented when a thread releases the semaphore. 如果计数为零,则后续请求会阻塞,直到其他线程释放信号量。When the count is zero, subsequent requests block until other threads release the semaphore. 当所有线程都已释放信号量后,计数将达到创建信号量时指定的最大值。When all threads have released the semaphore, the count is at the maximum value specified when the semaphore was created. 如果编程错误导致线程在此时调用 Semaphore.Release 方法,则将引发 SemaphoreFullExceptionIf a programming error causes a thread to call the Semaphore.Release method at this point, a SemaphoreFullException is thrown.

备注

Semaphore 类不会对 WaitHandle.WaitOneSemaphore.Release 方法的调用强制执行线程标识。The Semaphore class does not enforce thread identity on calls to the WaitHandle.WaitOne and Semaphore.Release methods. 调用 WaitOne 的同一线程不需要调用 ReleaseIt is not necessary for the same thread that called WaitOne to call Release.

SemaphoreFullException 不一定表示发生异常的代码有问题。SemaphoreFullException does not necessarily indicate a problem with the code where the exception occurred. 请考虑以下方案:线程 A 和线程 B 输入的信号量的最大计数为2。Consider the following scenario: Thread A and thread B enter a semaphore that has a maximum count of two. 线程 B 中的编程错误导致调用 Release 两次,以便信号量上的计数已满。A programming error in thread B causes it to call Release twice, so that the count on the semaphore is full. 因此,当线程 A 最终调用 Release时,将引发 SemaphoreFullExceptionAs a result, when thread A eventually calls Release, a SemaphoreFullException is thrown.

有关 SemaphoreFullException 类的实例的初始属性值列表,请参见 SemaphoreFullException() 构造函数。For a list of initial property values for an instance of the SemaphoreFullException class, see the SemaphoreFullException() constructor.

构造函数

SemaphoreFullException()

使用默认值初始化 SemaphoreFullException 类的新实例。Initializes a new instance of the SemaphoreFullException class with default values.

SemaphoreFullException(SerializationInfo, StreamingContext)

使用序列化数据初始化 SemaphoreFullException 类的新实例。Initializes a new instance of the SemaphoreFullException class with serialized data.

SemaphoreFullException(String)

用指定的错误消息初始化 SemaphoreFullException 类的新实例。Initializes a new instance of the SemaphoreFullException class with a specified error message.

SemaphoreFullException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 SemaphoreFullException 类的新实例。Initializes a new instance of the SemaphoreFullException class with a specified error message and a reference to the inner exception that is the cause of this exception.

属性

Data

获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(继承自 Exception)
HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(继承自 Exception)
HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(继承自 Exception)
Message

获取描述当前异常的消息。Gets a message that describes the current exception.

(继承自 Exception)
Source

获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(继承自 Exception)
StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(继承自 Exception)
TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(继承自 Exception)

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(继承自 Exception)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

当在派生类中重写时,用关于异常的信息设置 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(继承自 Exception)
GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(继承自 Exception)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

创建并返回当前异常的字符串表示形式。Creates and returns a string representation of the current exception.

(继承自 Exception)

事件

SerializeObjectState

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(继承自 Exception)

适用于

另请参阅