首页 > 技术文章 > string 线程安全

liuqiyun 2018-08-08 19:42 原文


线程安全:
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()); } } }

线程不安全:

 

线程不安全:

 

 

 

 线程不安全;

 

线程安全:

线程不安全:

 

线程安全:

 

 

 线程安全:

 

 线程安全:

线程安全:

线程不安全:

线程安全:

线程不安全:

原理:https://blog.csdn.net/soonfly/article/details/70147205

https://www.jianshu.com/p/af6eb8d3d4bf

 

推荐阅读