Java終止線程實例和stop()方法源碼閱讀
了解線程
概念
線程 是程序中的執(zhí)行線程。Java 虛擬機允許應用程序并發(fā)地運行多個執(zhí)行線程。
線程特點
擁有狀態(tài),表示線程的狀態(tài),同一時刻中,JVM中的某個線程只有一種狀態(tài);
·NEW
尚未啟動的線程(程序運行開始至今一次未啟動的線程)
·RUNNABLE
可運行的線程,正在JVM中運行,但它可能在等待其他資源,如CPU。
·BLOCKED
阻塞的線程,等待某個鎖允許它繼續(xù)運行
·WAITING
無限等待(再次運行依賴于讓它進入該狀態(tài)的線程執(zhí)行某個特定操作)
·TIMED_WAITING
定時等待(再次運行依賴于讓它進入該狀態(tài)的線程在指定等待時間內某個特定操作)
·TERMINATED
已退出的線程
擁有優(yōu)先級,決定線程的執(zhí)行順序;
1至10之間的整數,默認數值為5。數值越高,執(zhí)行的幾率越高,優(yōu)先級并不能決定線程的執(zhí)行順序。
子線程的優(yōu)先級默認同父線程的一樣。
注意,當以下情況發(fā)生時,JVM將停止執(zhí)行所有線程:
Runtime(運行時)的exit ()方法被調用并且該方法的調用被Security Manager所允許;
所有的“非守護線程”都已停止運行(無論時正常停止還是一場停止);
可以被標記為守護程序(Daemon)
守護線程的子線程仍是守護線程;
守護線程也就是“后臺線程”,一般用來執(zhí)行后臺任務,而用戶線程一般用戶執(zhí)行用戶級任務。
終止線程的方法
1.使用退出標志,使線程正常退出,也就是當run方法完成后線程終止。
當run方法執(zhí)行完后,線程就會退出。但有時run方法是永遠不會結束的。如在服務端程序中使用線程進行監(jiān)聽客戶端請求,或是其他的需要循環(huán)處理的任務。在這種情況下,一般是將這些任務放在一個循環(huán)中,如while循環(huán)。如果想讓循環(huán)永遠運行下去,可以使用while(true){……}來處理。但要想使while循環(huán)在某一特定條件下退出,最直接的方法就是設一個boolean類型的標志,并通過設置這個標志為true或false來控制while循環(huán)是否退出。下面給出了一個利用退出標志終止線程的例子。
FlagExitThread.java
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
package com.rainmonth;
/**
* Created by RandyZhang on 2017/3/23.
*/
public class DemoClient {
public static void main(String[] args) {
System.out.println("優(yōu)雅的終止線程實例");
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);
System.out.println("stop application...");
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
輸出結果:
打印了一大堆I'm running之后線程退出。
2.使用stop方法強行終止線程(這個方法不推薦使用,因為stop和suspend、resume一樣,也可能發(fā)生不可預料的結果)。
顯示調用stop()方法。源碼中關于stop() 的描述如下:
/* * 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()終止一個線程會釋放它已經鎖定的所有監(jiān)視器(這將導致沿堆棧向上傳播為檢查的ThreadDeath異常被拋出),若此時之前受這些被釋放的監(jiān)視器保護的對象存在不一致性,并且這些對象對其他線程可見,這就會導致一些意想不到的后果。stop操作應該有哪些僅僅只需要修改某些代碼就可以指示目標線程應該停止運行的代碼來取代(方法一就是這種方式)。如果目標線程由于等待某一條件(如某個條件變量)等待很長時間,我們應該使用interrupt方法來中斷該等待(方法三就是這種方式)。
3.使用interrupt方法中斷線程。
interrupt字面上是終止的意思,但不要試圖通過調用interrupt來終止線程,因為有時即使你調用了該方法,線程仍然會繼續(xù)執(zhí)行,可以注釋掉上面的exitByFlag(),開啟exitByInterrupt() 方法,發(fā)現及時調用了interrupt()方法,仍在一直輸出I'm running…(不同系統(tǒng)及CPU結果可能有所不同),可見采用interrupt方式也是不安全的。
總結
根據以上的分析,最值得推薦的方式是第一種,我們可以用共享變量(shared variable)的方式來設置標志,并發(fā)出信號,通知線程必須終止。當然對這個共享變量的操作我們必須保證是同步的。
以上就是本文關于Java終止線程實例和stop()方法源碼閱讀的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
Java調用ChatGPT(基于SpringBoot和Vue)實現可連續(xù)對話和流式輸出的ChatGPT API
這篇文章主要介紹了Java調用ChatGPT(基于SpringBoot和Vue),實現可連續(xù)對話和流式輸出的ChatGPT API(可自定義實現AI助手),文中代碼示例介紹的非常詳細,感興趣的朋友可以參考下2023-04-04
通過FeignClient調用微服務提供的分頁對象IPage報錯的解決
這篇文章主要介紹了通過FeignClient調用微服務提供的分頁對象IPage報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
eclipse/intellij idea 查看java源碼和注釋方法
下面小編就為大家?guī)硪黄猠clipse/intellij idea 查看java源碼和注釋方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

