static void Main(string[] args) { //线程启动 //ThreadingText t = new ThreadingText(); //Thread myThread = new Thread(new ThreadStart(t.beta)); //myThread.Start(); //if (myThread.IsAlive) { // Thread.Sleep(10); // Console.WriteLine(myThread.ThreadState.ToString()); // myThread.Abort(); // Console.WriteLine(myThread.ThreadState.ToString()); // myThread.Join(); // Console.WriteLine(myThread.ThreadState.ToString()); // Console.WriteLine(); // Console.WriteLine("Alpha.Beta has finished"); // float i = 54564543543543545341.1f; // int s = (int)i; // Console.WriteLine(s); // Console.ReadLine(); //} //余额 //Balance b = new Balance(); //for (int i = 0; i < 10; i++) { // Thread s = new Thread(new ThreadStart(b.doS)); // threads[i] = s; // threads[i].Name = i.ToString()+"号线程"; // threads[i].Start(); //} //Console.ReadLine(); //生产者与消费者 //Cell c = new Cell(); //GetCell getc = new GetCell(c, 20); //SetCell setc = new SetCell(c, 20); //Thread g = new Thread(new ThreadStart(getc.doGetCell)); //Thread s = new Thread(new ThreadStart(setc.doSetCell)); //s.Start(); //g.Start(); //Console.ReadLine(); //定时器 //TimerCallback callback = new TimerCallback(checkStatus); //TimerTest s = new TimerTest(); //Timer tim = new Timer(checkStatus, s, 0, 1000); //s.tim = tim; //while (s.tim != null) { } //Console.WriteLine("Timer example done."); //Console.ReadLine(); //互斥对象 MutexTest.test(); }
public static void test() { Console.WriteLine("开始"); //创建一个Mutex对象,并且命名为MyMutex gM1 = new Mutex(true, "MyMutex"); //创建一个未命名的Mutex 对象. gM2 = new Mutex(true); Console.WriteLine(" - Main Owns gM1 and gM2"); AutoResetEvent[] evs = new AutoResetEvent[4]; evs[0] = Event1; //为后面的线程t1,t2,t3,t4定义AutoResetEvent对象 evs[1] = Event2; evs[2] = Event3; evs[3] = Event4; MutexTest t = new MutexTest(); Thread t1 = new Thread(new ThreadStart(t.t1Start)); Thread t2 = new Thread(new ThreadStart(t.t2Start)); Thread t3 = new Thread(new ThreadStart(t.t3Start)); Thread t4 = new Thread(new ThreadStart(t.t4Start)); t1.Start(); t2.Start(); t3.Start(); t4.Start(); Thread.Sleep(2000); Console.WriteLine("主线程释放gm1 与 gm2"); gM1.ReleaseMutex(); //线程t2,t3结束条件满足 gM2.ReleaseMutex(); //线程t1,t4结束条件满足 //等待所有四个线程结束 WaitHandle.WaitAll(evs); Console.WriteLine(" Mutex Sample"); Console.ReadLine(); }