1 区别:
a 、 moniter 继承的积累为object,sleep 继承 thread 类
b、moniter.wait(),会阻塞线程,阻塞的同时但会释放锁,再次获得锁的时候,需要monitor.Pulse唤醒
c、 thread.sleep 是当前的线程休眠,时间到后,自动恢复 继续往下执行
例如:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Wait和Sleep { class Program { static object locker = new object(); static Thread thread1 = new Thread(TestTwo); static void Main(string[] args) { Thread thread = new Thread(TestWait); thread.Start(); lock (locker) { Console.WriteLine($"线程拥有锁"); Console.WriteLine($"线程开始休眠"); for (int i = 0; i < 3; i++) { Thread.Sleep(1000); Console.WriteLine($"Main{i}"); } Console.WriteLine($"线程休眠结束"); } Console.ReadLine(); } static void TestWait() { Console.WriteLine($"TestWait"); //这里会等待locker 释放 lock (locker) { Console.WriteLine($"开始wait"); thread1.Start(); Console.WriteLine($"开始wait并且释放锁,阻塞当前线程向下执行,直到再一次获取锁"); var satus = Monitor.Wait(locker, 2000); //释放对象上的锁并阻止当前线程,直到它重新获取该锁。 如果已用指定的超时时间间隔,则线程进入就绪队列。 if (!satus) { Console.WriteLine("超时"); } while (satus) { Console.WriteLine($"TestWait已获取锁,继续执行"); Console.WriteLine("TestWait通知释放锁"); Monitor.Pulse(locker);// Console.WriteLine("TestWait通知完成"); break; } } } static void TestTwo() { lock (locker) { Console.WriteLine("TestTwo获取锁"); for (int i = 0; i < 3; i++) { Thread.Sleep(1000); Console.WriteLine($"TestTwo{i}"); } Console.WriteLine("通知下一个线程获取该锁"); Monitor.Pulse(locker); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Wait和Sleep { class Program { static object locker = new object(); static Thread thread1 = new Thread(TestTwo); static void Main(string[] args) { Thread thread = new Thread(TestWait); thread.Start(); lock (locker) { Console.WriteLine($"线程拥有锁"); Console.WriteLine($"线程开始休眠"); for (int i = 0; i < 3; i++) { Thread.Sleep(1000); Console.WriteLine($"Main{i}"); } Console.WriteLine($"线程休眠结束"); } Console.ReadLine(); } static void TestWait() { Console.WriteLine($"TestWait"); //这里会等待locker 释放 lock (locker) { Console.WriteLine($"开始wait"); thread1.Start(); Console.WriteLine($"开始wait并且释放锁,阻塞当前线程向下执行,直到再一次获取锁"); var satus = Monitor.Wait(locker, 2000); //释放对象上的锁并阻止当前线程,直到它重新获取该锁。 如果已用指定的超时时间间隔,则线程进入就绪队列。 if (!satus) { Console.WriteLine("超时"); } while (satus) { Console.WriteLine($"TestWait已获取锁,继续执行"); Console.WriteLine("TestWait通知释放锁"); Monitor.Pulse(locker);// Console.WriteLine("TestWait通知完成"); break; } } } static void TestTwo() { lock (locker) { Console.WriteLine("TestTwo获取锁"); for (int i = 0; i < 3; i++) { Thread.Sleep(1000); Console.WriteLine($"TestTwo{i}"); } Console.WriteLine("通知下一个线程获取该锁"); Monitor.Pulse(locker); } } } }