Public int Release()
public int Release(int releaseCount)
Release () , , releaseCount. , .
WaitOne () Release (). WaitOne () Release () . , Release(int ), , WaitOne ().
, . MyThread MyThread. , .
// .
Using System;
Using System.Threading;
// // , class MyThread {
Public Thread Thrd;
// , // , static Semaphore sem = new Semaphore(2, 2);
public MyThread(string name) {
Thrd = new Thread(this.Run);
Thrd.Name = name;
Thrd. Start ();
}
// , void Run() {
Console.WriteLine(Thrd.Name + " .");
Sem.WaitOne();
Console.WriteLine(Thrd.Name + " .");
for(char ch='A'; ch < 'D'; ch++) {
Console.,WriteLine (Thrd.Name + ": " + ch + " ");
Thread.Sleep(500);
}
Console.WriteLine(Thrd.Name + " .");
// , sem.Release();
}
}
class SemaphoreDemo { static void Main() {
// .
Mtl.Thrd.Join(); mt2.Thrd.Join(); mt3.Thrd.Join();
}
}
MyThread sem, .
static Semaphore sem = new Semaphore(2f 2);
, .
, MyThread. Run () , sem . , . , . In Main () . , . , .
#1 .
#1 .
#1:
#2 .
|
|
#2 .
#2:
#3 .
#1: #2: #1: #2:
#1 .
#3 .
#3:
#2 .
#3: #3:
#3 .
, , , . , - . . Semaphore, .
public Semaphore(int initialCount, int maximumCountf string ) public Semaphore(int initialCount, int maximumCount, string , out bool createdNew)
, . , , , , initialCount maximumCount. , initialCount maximumCount . createdNew true, . ini tialCount maximumCount . createdNew false, , initialCount maximumCount . Semaphore, SemaphoreSecurity. .
C# : . : . ManualResetEvent AutoResetEvent . EventWaitHandle, , , . , , .