1.Thread中的一些靜態方法併發
給全部的線程分配一個數據槽,存放數據ide
GetData性能
SetData學習
////分配已經命名的槽位 //var solt=Thread.AllocateNamedDataSlot("username"); //// var solt = Thread.AllocateDataSlot(); ////在主線程中給槽位賦值,只能在主線程讀取,子線程讀取不到 //Thread.SetData(solt, "Hello Word!"); //var t = new Thread(()=> { // var obj = Thread.GetData(solt); // Console.WriteLine("當前工做的線程:{0}",obj); //}); //t.Start(); //var obj2= Thread.GetData(solt); //Console.WriteLine("主線程:{0}", obj2);
2.Thread中的一些靜態方法的使用spa
變量=>Thread的關係t1,t2線程
t1,t2 共享變量 public 有鎖code
t1,t2各有一個變量 internel 無鎖blog
3.性能提高版本 ThreadState資源
[ThreadStatic] static string username = string.Empty; static void Main(string[] args) { username = "Hello Word!"; var t = new Thread(() => { Console.WriteLine("當前工做的線程:{0}", username); }); t.Start(); Console.WriteLine("主線程:{0}", username); Console.Read();
4.ThreadLocal:也是用來作線程可見性string
static void Main(string[] args) { ThreadLocal<string> local = new ThreadLocal<string>(); local.Value = "Hello World!"; //username = "Hello Word!"; var t = new Thread(() => { Console.WriteLine("當前工做的線程:{0}", local.Value); }); t.Start(); Console.WriteLine("主線程:{0}", local.Value); Console.Read(); }
從筆記的中,學習總結: