for (int i = 1; i <= TestMutiThrd.numIterations; i++)
{
Console.WriteLine("买书线程:书本{0}", i);
TestMutiThrd.number = i;
//Signal that a value has been written.
TestMutiThrd.SekEvent.Set();//解锁扫描线程
TestMutiThrd.buyResetEvent.WaitOne();//等待买书所有线程执行结束,才能继续
Thread.Sleep(0);
}
Thread.Sleep(1000);
Console.ReadLine();
SeekBookThread.Abort();
payMoneyThread.Abort();
getBookThread.Abort();
}
}
public class TestMutiThrd
{
public const int numIterations = 50;
//买书
public static AutoResetEvent buyResetEvent = new AutoResetEvent(false);
//扫描
public static AutoResetEvent SekEvent = new AutoResetEvent(false);
//付钱
public static AutoResetEvent PayEvent = new AutoResetEvent(false);
//取书
public static AutoResetEvent GetEvent = new AutoResetEvent(false);
//付钱和取书
public static ManualResetEvent PayGetEvent = new ManualResetEvent(false);
public static ReaderWriterLock RWLock = new ReaderWriterLock();
//static ManualResetEvent myResetEvent = new ManualResetEvent(false);
//static ManualResetEvent ChangeEvent = new ManualResetEvent(false);
public static int number; //这是关键资源
public static TestMutiThrd OTestMutiThrd = new TestMutiThrd();
public int NumIterations
{
get
{
return numIterations;
}
}