這篇文章主要是用最簡(jiǎn)單的例子,總結(jié)下多線程調(diào)用函數(shù)的相關(guān)注意點(diǎn),重點(diǎn)偏向應(yīng)用和記憶。
1.多線程調(diào)用無(wú)參函數(shù)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace 多線程 { class Program { static void Main(string[] args) { Console.WriteLine("主線程開(kāi)始"); Thread t = new Thread(new ThreadStart(ShowTime));//注意ThreadStart委托的定義形式 t.Start();//線程開(kāi)始,控制權(quán)返回Main線程 Console.WriteLine("主線程繼續(xù)執(zhí)行"); //while (t.IsAlive == true) ; Thread.Sleep(1000); t.Abort(); t.Join();//阻塞Main線程,直到t終止 Console.WriteLine("--------------"); Console.ReadKey(); } static void ShowTime() { while (true) { Console.WriteLine(DateTime.Now.ToString()); } } } }
注意ThreadStart委托的定義如下:
可見(jiàn)其對(duì)傳遞進(jìn)來(lái)的函數(shù)要求是:返回值void,無(wú)參數(shù)。
2.多線程調(diào)用帶參函數(shù)(兩種方法)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace 多線程2_帶參數(shù) { class Program { static void Main(string[] args) { Console.WriteLine("Main線程開(kāi)始"); Thread t = new Thread(new ParameterizedThreadStart(DoSomething));//注意ParameterizedThreadStart委托的定義形式 t.Start(new string[]{"Hello","World"}); Console.WriteLine("Main線程繼續(xù)執(zhí)行"); Thread.Sleep(1000); t.Abort(); t.Join();//阻塞Main線程,直到t終止 Console.ReadKey(); } static void DoSomething(object s) { string[] strs = s as string[]; while (true) { Console.WriteLine("{0}--{1}",strs[0],strs[1]); } } } }
注意ParameterizedThreadStart委托的定義如下:
可見(jiàn)其對(duì)傳入函數(shù)的要求是:返回值void,參數(shù)個(gè)數(shù)1,參數(shù)類型object
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace 多線程2_帶參數(shù)2 { class Program { static void Main(string[] args) { Guest guest = new Guest() { Name="Hello", Age=99 }; Thread t = new Thread(new ThreadStart(guest.DoSomething));//注意ThreadStart委托的定義形式 t.Start(); Thread.Sleep(1000); t.Abort(); t.Join();//阻塞Main線程,直到t終止 Console.ReadKey(); } } // class Guest { public string Name { get; set; } public int Age { get; set; } public void DoSomething() { while (true) { Console.WriteLine("{0}--{1}", Name, Age); } } } }
這個(gè)還是使用ThreadStart委托,對(duì)方法進(jìn)行了一個(gè)封裝。
兩種方法,可隨意選擇,第一種貌似簡(jiǎn)潔一點(diǎn)。
3.線程同步
線程同步的方法有很多很多種volatile、Lock、InterLock、Monitor、Mutex、ReadWriteLock...
這里用lock說(shuō)明問(wèn)題:在哪里同步,用什么同步,同步誰(shuí)?
首先感受下不同步會(huì)出現(xiàn)的問(wèn)題:
代碼就是下面的代碼去掉lock塊。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace 多線程3_同步2 { class Program { static object obj = new object();//同步用 static int balance = 500; static void Main(string[] args) { Thread t1 = new Thread(new ThreadStart(Credit)); t1.Start(); Thread t2 = new Thread(new ThreadStart(Debit)); t2.Start(); Console.ReadKey(); } static void Credit() { for (int i = 0; i < 15; i++) { lock (obj) { balance += 100; Console.WriteLine("After crediting,balance is {0}", balance); } } } static void Debit() { for (int i = 0; i < 15; i++) { lock (obj) { balance -= 100; Console.WriteLine("After debiting,balance is {0}", balance); } } } } }
小結(jié):多線程調(diào)用函數(shù)就是這樣。在Winform中,控件綁定到特定的線程,從另一個(gè)線程更新控件,不應(yīng)該直接調(diào)用該控件的成員,這個(gè)非常有