了解線程
概念
線程 是程序中的執行線程。Java 虛擬機允許應用程序并發地運行多個執行線程。
線程特點
擁有狀態,表示線程的狀態,同一時刻中,JVM中的某個線程只有一種狀態;
·NEW
尚未啟動的線程(程序運行開始至今一次未啟動的線程)
·RUNNABLE
可運行的線程,正在JVM中運行,但它可能在等待其他資源,如CPU。
·BLOCKED
阻塞的線程,等待某個鎖允許它繼續運行
·WAITING
無限等待(再次運行依賴于讓它進入該狀態的線程執行某個特定操作)
·TIMED_WAITING
定時等待(再次運行依賴于讓它進入該狀態的線程在指定等待時間內某個特定操作)
·TERMINATED
已退出的線程
擁有優先級,決定線程的執行順序;
1至10之間的整數,默認數值為5。數值越高,執行的幾率越高,優先級并不能決定線程的執行順序。
子線程的優先級默認同父線程的一樣。
注意,當以下情況發生時,JVM將停止執行所有線程:
Runtime(運行時)的exit ()方法被調用并且該方法的調用被Security Manager所允許;
所有的“非守護線程”都已停止運行(無論時正常停止還是一場停止);
可以被標記為守護程序(Daemon)
守護線程的子線程仍是守護線程;
守護線程也就是“后臺線程”,一般用來執行后臺任務,而用戶線程一般用戶執行用戶級任務。
終止線程的方法
1.使用退出標志,使線程正常退出,也就是當run方法完成后線程終止。
當run方法執行完后,線程就會退出。但有時run方法是永遠不會結束的。如在服務端程序中使用線程進行監聽客戶端請求,或是其他的需要循環處理的任務。在這種情況下,一般是將這些任務放在一個循環中,如while循環。如果想讓循環永遠運行下去,可以使用while(true){……}來處理。但要想使while循環在某一特定條件下退出,最直接的方法就是設一個boolean類型的標志,并通過設置這個標志為true或false來控制while循環是否退出。下面給出了一個利用退出標志終止線程的例子。
FlagExitThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.rainmonth; /** * Created by RandyZhang on 2017/3/23. */ public class FlagExitThread extends Thread { public volatile Boolean isExit = false ; public FlagExitThread(String name) { super (name); } @Override public void run() { while (!isExit) { System.out.println( "I'm running" ); } } } |
DemoClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package com.rainmonth; /** * Created by RandyZhang on 2017/3/23. */ public class DemoClient { public static void main(String[] args) { System.out.println( "優雅的終止線程實例" ); exitByFlag(); // exitByInterrupt(); } private static void exitByFlag() { FlagExitThread flagExitThread = new FlagExitThread(FlagExitThread. class .getSimpleName()); flagExitThread.start(); try { Thread.sleep( 1000 ); flagExitThread.isExit = true ; flagExitThread.join(); System.out.println( "線程退出" ); } catch (InterruptedException e) { e.printStackTrace(); } } private static void exitByInterrupt() { FlagExitThread flagExitThread = new FlagExitThread(FlagExitThread. class .getSimpleName()); System.out.println( "flagExitThread running..." ); flagExitThread.start(); try { Thread.sleep( 1500 ); System.out.println( "flagExitThread interrupted..." ); flagExitThread.interrupt(); Thread.sleep( 1500 ); } catch (InterruptedException e) { e.printStackTrace(); } } } |
輸出結果:
打印了一大堆I'm running之后線程退出。
2.使用stop方法強行終止線程(這個方法不推薦使用,因為stop和suspend、resume一樣,也可能發生不可預料的結果)。
顯示調用stop()方法。源碼中關于stop() 的描述如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/* * This method is inherently unsafe. Stopping a thread with * Thread.stop causes it to unlock all of the monitors that it * has locked (as a natural consequence of the unchecked * <code>ThreadDeath</code> exception propagating up the stack). If * any of the objects previously protected by these monitors were in * an inconsistent state, the damaged objects become visible to * other threads, potentially resulting in arbitrary behavior. Many * uses of <code>stop</code> should be replaced by code that simply * modifies some variable to indicate that the target thread should * stop running. The target thread should check this variable * regularly, and return from its run method in an orderly fashion * if the variable indicates that it is to stop running. If the * target thread waits for long periods (on a condition variable, * for example), the <code>interrupt</code> method should be used to * interrupt the wait. */ |
大意就是說,該方法的不安全性時固有的。調用stop()終止一個線程會釋放它已經鎖定的所有監視器(這將導致沿堆棧向上傳播為檢查的ThreadDeath異常被拋出),若此時之前受這些被釋放的監視器保護的對象存在不一致性,并且這些對象對其他線程可見,這就會導致一些意想不到的后果。stop操作應該有哪些僅僅只需要修改某些代碼就可以指示目標線程應該停止運行的代碼來取代(方法一就是這種方式)。如果目標線程由于等待某一條件(如某個條件變量)等待很長時間,我們應該使用interrupt方法來中斷該等待(方法三就是這種方式)。
3.使用interrupt方法中斷線程。
interrupt字面上是終止的意思,但不要試圖通過調用interrupt來終止線程,因為有時即使你調用了該方法,線程仍然會繼續執行,可以注釋掉上面的exitByFlag(),開啟exitByInterrupt() 方法,發現及時調用了interrupt()方法,仍在一直輸出I'm running…(不同系統及CPU結果可能有所不同),可見采用interrupt方式也是不安全的。
總結
根據以上的分析,最值得推薦的方式是第一種,我們可以用共享變量(shared variable)的方式來設置標志,并發出信號,通知線程必須終止。當然對這個共享變量的操作我們必須保證是同步的。
以上就是本文關于Java終止線程實例和stop()方法源碼閱讀的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/rainmonth/article/details/65445467