string 线程安全


线程安全:
class
Program { public delegate void MyDelegate(string aa); static void Main(string[] args) { MyDelegate dele = new MyDelegate(LockTest); MyDelegate dele1 = new MyDelegate(LockTest); dele.BeginInvoke("aa", null,null); dele1.BeginInvoke("aa", null, null); Console.ReadKey(); } private static void LockTest(string aa) { string thisServiceLock = string.Intern(aa+"_lockCheck"); lock (thisServiceLock) { Console.WriteLine("线程ID" + Thread.CurrentThread.ManagedThreadId.ToString()); Thread.Sleep(10000); Console.WriteLine("线程ID" + Thread.CurrentThread.ManagedThreadId.ToString()); } } }

线程不安全:

线程不安全:

 线程不安全;

线程安全:

线程不安全:

线程安全:

 线程安全:

 线程安全:

线程安全:

线程不安全:

猜你喜欢

转载自www.cnblogs.com/liuqiyun/p/9445231.html