SemaphoreFullException Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Pengecualian yang dilemparkan ketika Release metode dipanggil pada semaphore yang jumlahnya sudah maksimal.
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
- Warisan
- Warisan
- Atribut
Contoh
Contoh kode berikut menunjukkan bagaimana kesalahan pemrograman dalam satu utas dapat menyebabkan SemaphoreFullException di utas lain: Dua utas memasukkan semaphore. Utas kedua merilis semaphore dua kali, sementara utas pertama masih menjalankan tugasnya. Ketika utas pertama selesai dan melepaskan semaphore, jumlah semaphore sudah penuh dan pengecualian dilemparkan.
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.
'
Keterangan
Hitungan pada semaphore ditentukan setiap kali utas memasuki semaphore, dan bertambah ketika utas melepaskan semaphore. Ketika jumlahnya nol, permintaan berikutnya memblokir hingga utas lain merilis semaphore. Ketika semua utas telah merilis semaphore, jumlahnya adalah pada nilai maksimum yang ditentukan ketika semaphore dibuat. Jika kesalahan pemrograman menyebabkan utas memanggil Semaphore.Release metode pada titik ini, akan SemaphoreFullException dilemparkan.
Note
Kelas Semaphore tidak memberlakukan identitas utas pada panggilan ke WaitHandle.WaitOne metode dan Semaphore.Release . Tidak perlu untuk utas yang sama yang dipanggil WaitOne untuk memanggil Release.
SemaphoreFullException tidak selalu menunjukkan masalah dengan kode tempat pengecualian terjadi. Pertimbangkan skenario berikut: Thread A dan thread B masukkan semaphore yang memiliki jumlah maksimum dua. Kesalahan pemrograman di utas B menyebabkannya memanggil Release dua kali, sehingga hitungan pada semaphore penuh. Akibatnya, ketika utas ReleaseA akhirnya memanggil , dilemparkan SemaphoreFullException .
Untuk daftar nilai properti awal untuk instans kelas SemaphoreFullException, lihat konstruktor SemaphoreFullException().
Konstruktor
| Nama | Deskripsi |
|---|---|
| SemaphoreFullException() |
Menginisialisasi instans SemaphoreFullException baru kelas dengan nilai default. |
| SemaphoreFullException(SerializationInfo, StreamingContext) |
Kedaluwarsa.
Menginisialisasi instans SemaphoreFullException baru kelas dengan data berseri. |
| SemaphoreFullException(String, Exception) |
Menginisialisasi instans SemaphoreFullException baru kelas dengan pesan kesalahan tertentu dan referensi ke pengecualian dalam yang merupakan penyebab pengecualian ini. |
| SemaphoreFullException(String) |
Menginisialisasi instans baru kelas SemaphoreFullException dengan pesan kesalahan tertentu. |
Properti
| Nama | Deskripsi |
|---|---|
| Data |
Mendapatkan kumpulan pasangan kunci/nilai yang memberikan informasi tambahan yang ditentukan pengguna tentang pengecualian. (Diperoleh dari Exception) |
| HelpLink |
Mendapatkan atau mengatur tautan ke file bantuan yang terkait dengan pengecualian ini. (Diperoleh dari Exception) |
| HResult |
Mendapatkan atau mengatur HRESULT, nilai numerik berkode yang ditetapkan ke pengecualian tertentu. (Diperoleh dari Exception) |
| InnerException |
Mendapatkan instans Exception yang menyebabkan pengecualian saat ini. (Diperoleh dari Exception) |
| Message |
Mendapatkan pesan yang menjelaskan pengecualian saat ini. (Diperoleh dari Exception) |
| Source |
Mendapatkan atau mengatur nama aplikasi atau objek yang menyebabkan kesalahan. (Diperoleh dari Exception) |
| StackTrace |
Mendapatkan representasi string dari bingkai langsung pada tumpukan panggilan. (Diperoleh dari Exception) |
| TargetSite |
Mendapatkan metode yang melemparkan pengecualian saat ini. (Diperoleh dari Exception) |
Metode
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
| GetBaseException() |
Ketika ditimpa di kelas turunan, mengembalikan Exception yang merupakan akar penyebab dari satu atau beberapa pengecualian berikutnya. (Diperoleh dari Exception) |
| GetHashCode() |
Berfungsi sebagai fungsi hash default. (Diperoleh dari Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Kedaluwarsa.
Saat ditimpa di kelas turunan, mengatur SerializationInfo dengan informasi tentang pengecualian. (Diperoleh dari Exception) |
| GetType() |
Mendapatkan jenis runtime instans saat ini. (Diperoleh dari Exception) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Membuat dan mengembalikan representasi string dari pengecualian saat ini. (Diperoleh dari Exception) |
Acara
| Nama | Deskripsi |
|---|---|
| SerializeObjectState |
Kedaluwarsa.
Terjadi ketika pengecualian diserialisasikan untuk membuat objek status pengecualian yang berisi data berseri tentang pengecualian. (Diperoleh dari Exception) |