2024年1月31日发(作者:)
(1000); ++; ine("In " + + " is " + ); number--; } while (number > 0); ine( + "释放 the nmutex"); // 释放 eMutex(); } } class DecThread { int number; public Thread thrd; public DecThread(string name, int n) { thrd = new Thread(); number = n; = name; (); } void run() { ine( + "正在等待 the mutex"); //申请 e(); ine( + "申请到 the mutex"); do { (1000); --; ine("In " + + " is " + ); number--; } while (number > 0); ine( + "释放 the nmutex"); // 释放 eMutex(); } } class Program { static void Main(string[] args) { IncThread mthrd1 = new IncThread("IncThread thread ", 5); DecThread mthrd2 = new DecThread("DecThread thread ", 5); (); (); } }}
namespace MonitorLockMutex{ class Program { Thread thread1 = null; Thread thread2 = null; Mutex mutex = null; static void Main(string[] args) { Program p = new Program(); ead(); ne(); } public Program() { mutex = new Mutex(); thread1 = new Thread(new ThreadStart(thread1Func)); thread2 = new Thread(new ThreadStart(thread2Func)); } public void RunThread() { (); (); } private void thread1Func() { for (int count = 0; count < 10; count++) { TestFunc("Thread1 have run " + ng() + " times"); //暂停500ms (500); } } private void thread2Func() { for (int count = 0; count < 10; count++) {
TestFunc("Thread2 have run " + ng() + " times"); //暂停1500ms (1500); }
} private void TestFunc(string str) {
ine("{0} {1}", str, econd);
} }


发布评论