SemaphoreFullException Klasa

Definicja

Wyjątek, który jest zgłaszany, gdy Release metoda jest wywoływana na semafor, którego liczba jest już maksymalna.

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
Dziedziczenie
SemaphoreFullException
Dziedziczenie
SemaphoreFullException
Atrybuty

Przykłady

Poniższy przykład kodu pokazuje, jak błąd programowania w jednym wątku może prowadzić do innego SemaphoreFullException wątku: dwa wątki wchodzą w semafor. Drugi wątek zwalnia semafor dwa razy, podczas gdy pierwszy wątek nadal wykonuje swoje zadanie. Po zakończeniu pierwszego wątku i uwolnieniu semafora liczba semaforów jest już pełna i zgłaszany jest wyjątek.

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

Uwagi

Liczba semaforów jest dekrementowana za każdym razem, gdy wątek wchodzi do semafora i zwiększa się, gdy wątek zwalnia semafor. Gdy liczba jest równa zero, kolejne żądania blokują, dopóki inne wątki nie zwolnią semafora. Gdy wszystkie wątki zwolniły semafor, liczba jest na maksymalnej wartości określonej podczas tworzenia semafora. Jeśli błąd programowania powoduje, że wątek wywołuje Semaphore.Release metodę w tym momencie, SemaphoreFullException zgłaszany jest błąd.

Uwaga

Klasa Semaphore nie wymusza tożsamości wątku na wywołaniach metod WaitHandle.WaitOne i Semaphore.Release . Nie jest konieczne dla tego samego wątku, który wywołuje WaitOne metodę Release.

SemaphoreFullException nie musi wskazywać problemu z kodem, w którym wystąpił wyjątek. Rozważmy następujący scenariusz: Wątki A i wątek B wprowadzają semafor, który ma maksymalną liczbę dwóch. Błąd programowania w wątku B powoduje dwukrotne wywołanie Release , tak aby liczba semaforów była pełna. W rezultacie, gdy wątek A w końcu wywołuje Releaseelement , SemaphoreFullException jest zgłaszany.

Aby uzyskać listę początkowych wartości właściwości dla wystąpienia SemaphoreFullException klasy, zobacz SemaphoreFullException() konstruktor.

Konstruktory

SemaphoreFullException()

Inicjuje nowe wystąpienie klasy SemaphoreFullException z domyślnymi wartościami.

SemaphoreFullException(SerializationInfo, StreamingContext)

Inicjuje nowe wystąpienie klasy SemaphoreFullException z zserializowanymi danymi.

SemaphoreFullException(String)

Inicjuje SemaphoreFullException nowe wystąpienie klasy z określonym komunikatem o błędzie.

SemaphoreFullException(String, Exception)

Inicjuje nowe wystąpienie SemaphoreFullException klasy z określonym komunikatem o błędzie i odwołaniem do wyjątku wewnętrznego, który jest przyczyną tego wyjątku.

Właściwości

Data

Pobiera kolekcję par klucz/wartość, które zapewniają dodatkowe informacje zdefiniowane przez użytkownika dotyczące wyjątku.

(Odziedziczone po Exception)
HelpLink

Pobiera lub ustawia link do pliku pomocy skojarzonego z tym wyjątkiem.

(Odziedziczone po Exception)
HResult

Pobiera lub ustawia HRESULT, zakodowaną wartość liczbową przypisaną do określonego wyjątku.

(Odziedziczone po Exception)
InnerException

Exception Pobiera wystąpienie, które spowodowało bieżący wyjątek.

(Odziedziczone po Exception)
Message

Pobiera komunikat opisujący bieżący wyjątek.

(Odziedziczone po Exception)
Source

Pobiera lub ustawia nazwę aplikacji lub obiektu, który powoduje błąd.

(Odziedziczone po Exception)
StackTrace

Pobiera reprezentację ciągu natychmiastowych ramek na stosie wywołań.

(Odziedziczone po Exception)
TargetSite

Pobiera metodę, która zgłasza bieżący wyjątek.

(Odziedziczone po Exception)

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetBaseException()

Po przesłonięciu w klasie pochodnej funkcja zwraca Exception główną przyczynę co najmniej jednego kolejnego wyjątku.

(Odziedziczone po Exception)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetObjectData(SerializationInfo, StreamingContext)

Po zastąpieniu w klasie pochodnej ustawia SerializationInfo element z informacjami o wyjątku.

(Odziedziczone po Exception)
GetType()

Pobiera typ środowiska uruchomieniowego bieżącego wystąpienia.

(Odziedziczone po Exception)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Tworzy i zwraca reprezentację ciągu bieżącego wyjątku.

(Odziedziczone po Exception)

Zdarzenia

SerializeObjectState
Przestarzałe.

Występuje, gdy wyjątek jest serializowany w celu utworzenia obiektu stanu wyjątku, który zawiera serializowane dane dotyczące wyjątku.

(Odziedziczone po Exception)

Dotyczy

Zobacz też