判斷線程運行狀態(tài)的兩種方法
判斷線程運行狀態(tài)的方法有很多,如可以采用類似于對象計數(shù)器的方法,所謂對象計數(shù)器,就是一個對象被引用一次,這個計數(shù)器就加1,銷毀引用就減1,如果引用數(shù)為0,則垃圾搜集器就會對這些引用數(shù)為0的對象進行回收。
判斷線程運行狀態(tài)方法一:線程計數(shù)器
線程也可以采用計數(shù)器的方法,即為所有需要監(jiān)視的線程設一個線程計數(shù)器,每開始一個線程,在線程的執(zhí)行方法中為這個計數(shù)器加1,如果某個線程結束(在線程執(zhí)行方法的最后為這個計數(shù)器減1),為這個計數(shù)器減1。然后再開始一個線程,按著一定的時間間隔來監(jiān)視這個計數(shù)器,如是棕個計數(shù)器為0,說明所有的線程都結束了。當然,也可以不用這個監(jiān)視線程,而在每一個工作線程的最后(在為計數(shù)器減1的代碼的后面)來監(jiān)視這個計數(shù)器,也就是說,每一個工作線程在退出之前,還要負責檢測這個計數(shù)器。使用這種方法不要忘了同步這個計數(shù)器變量啊,否則會產(chǎn)生意想不到的后果。
判斷線程運行狀態(tài)方法二:使用Thread.join方法
join方法只有在線程結束時才繼續(xù)執(zhí)行下面的語句。可以對每一個線程調用它的join方法,但要注意,這個調用要在另一個線程里,而不要在主線程,否則程序會被阻塞的。
個人感覺這種方法比較好。
線程計數(shù)器方法演示:
- class ThreadCounter : MyThread
- {
- private static int count = 0;
- private int ms;
- private static void increment()
- {
- lock (typeof(ThreadCounter)) // 必須同步計數(shù)器
- {
- count++;
- }
- }
- private static void decrease()
- {
- lock (typeof(ThreadCounter))
- {
- count--;
- }
- }
- private static int getCount()
- {
- lock (typeof(ThreadCounter))
- {
- return count;
- }
- }
- public ThreadCounter(int ms)
- {
- this.ms = ms;
- }
- override public void run()
- {
- increment();
- Thread.Sleep(ms);
- Console.WriteLine(ms.ToString()+"毫秒任務結束");
- decrease();
- if (getCount() == 0)
- Console.WriteLine("所有任務結束");
- }
- }
- ThreadCounter counter1 = new ThreadCounter(3000);
- ThreadCounter counter2 = new ThreadCounter(5000);
- ThreadCounter counter3 = new ThreadCounter(7000);
- counter1.start();
- counter2.start();
- counter3.start();
上面的代碼雖然在大多數(shù)的時候可以正常工作,但卻存在一個隱患,就是如果某個線程,假設是counter1,在運行后,由于某些原因,其他的線程并未運行,在這種情況下,在counter1運行完后,仍然可以顯示出“所有任務結束”的提示信息,但是counter2和counter3還并未運行。為了消除這個隱患,可以將increment方法從run中移除,將其放到ThreadCounter的構造方法中,在這時,increment方法中的lock也可以去掉了。代碼如:
- public ThreadCounter(int ms)
- {
- this.ms = ms;
- increment();
- }
運行上面的程序后,將顯示如下圖的結果。
使用Thread.join方法演示
- private static void threadMethod(Object obj)
- {
- Thread.Sleep(Int32.Parse(obj.ToString()));
- Console.WriteLine(obj + "毫秒任務結束");
- }
- private static void joinAllThread(object obj)
- {
- Thread[] threads = obj as Thread[];
- foreach (Thread t in threads)
- t.Join();
- Console.WriteLine("所有的線程結束");
- }
- static void Main(string[] args)
- {
- Thread thread1 = new Thread(threadMethod);
- Thread thread2 = new Thread(threadMethod);
- Thread thread3 = new Thread(threadMethod);
- thread1.Start(3000);
- thread2.Start(5000);
- thread3.Start(7000);
- Thread joinThread = new Thread(joinAllThread);
- joinThread.Start(new Thread[] { thread1, thread2, thread3 });
- }
在運行上面的代碼后,將會得到和圖2同樣的運行結果。上述兩種方法都沒有線程數(shù)的限制,當然,仍然會受到操作系統(tǒng)和硬件資源的限制。
判斷線程運行狀態(tài)的兩大方法就向你介紹到這里,希望對你了解和學習線程運行狀態(tài)的判斷有所幫助。
【編輯推薦】