多线程12-ManualResetEventSlim
class Program
{
static ManualResetEventSlim manualRestEvnetSlim = new ManualResetEventSlim(false);
static void TravelThroughGates(string threadName,int second)
{
Console.WriteLine("{0} falls to sleep", threadName);
Thread.Sleep(TimeSpan.FromSeconds(second));
Console.WriteLine("{0} waits for the gate open", threadName);
manualRestEvnetSlim.Wait();
Console.WriteLine("{0} enter the gates", threadName);
}
static void Main()
{
var t1 = new Thread(() => TravelThroughGates("T1", 5));
var t2 = new Thread(() => TravelThroughGates("T2", 6));
var t3 = new Thread(() => TravelThroughGates("T3", 12));
t1.Start();
t2.Start();
t3.Start();
Thread.Sleep(TimeSpan.FromSeconds(6));
Console.WriteLine("the Gates is open");
manualRestEvnetSlim.Set();
Thread.Sleep(TimeSpan.FromSeconds(2));
manualRestEvnetSlim.Reset();
Console.WriteLine("the gate has been closed");
Thread.Sleep(TimeSpan.FromSeconds(10));
Console.WriteLine("the gates opend second times");
manualRestEvnetSlim.Set();
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("the gates closed angin");
manualRestEvnetSlim.Reset();
}
}
{
static ManualResetEventSlim manualRestEvnetSlim = new ManualResetEventSlim(false);
static void TravelThroughGates(string threadName,int second)
{
Console.WriteLine("{0} falls to sleep", threadName);
Thread.Sleep(TimeSpan.FromSeconds(second));
Console.WriteLine("{0} waits for the gate open", threadName);
manualRestEvnetSlim.Wait();
Console.WriteLine("{0} enter the gates", threadName);
}
static void Main()
{
var t1 = new Thread(() => TravelThroughGates("T1", 5));
var t2 = new Thread(() => TravelThroughGates("T2", 6));
var t3 = new Thread(() => TravelThroughGates("T3", 12));
t1.Start();
t2.Start();
t3.Start();
Thread.Sleep(TimeSpan.FromSeconds(6));
Console.WriteLine("the Gates is open");
manualRestEvnetSlim.Set();
Thread.Sleep(TimeSpan.FromSeconds(2));
manualRestEvnetSlim.Reset();
Console.WriteLine("the gate has been closed");
Thread.Sleep(TimeSpan.FromSeconds(10));
Console.WriteLine("the gates opend second times");
manualRestEvnetSlim.Set();
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("the gates closed angin");
manualRestEvnetSlim.Reset();
}
}