JAVA過關(guān)題-是否可以在線程運(yùn)行時(shí)將其Kill掉

轉(zhuǎn)自(侵刪):http://blog.163.com/xh_ding/blog/static/193903289201341685931689

如何停止java的線程一直是一個(gè)開發(fā)多線程程序常遇到的一個(gè)問題。也有好多人問過我,所以今天在這里總結(jié)一下希望可以讓更多的人知道在java中如何安全的結(jié)束一個(gè)正在運(yùn)行的線程。

在Java的多線程編程中,java.lang.Thread類型包含了一些列的方法start(), stop(), stop(Throwable) and suspend(), destroy() and resume()。通過這些方法,我們可以對(duì)線程進(jìn)行方便的操作,但是這些方法中,只有start()方法得到了保留。

在JDK幫助文檔以及Sun公司的一篇文章《Why are Thread.stop, Thread.suspend and Thread.resume Deprecated? 》中都講解了舍棄這些方法的原因。

那么,我們究竟應(yīng)該如何停止線程呢?這里我們介紹兩種方法:<wbr style="box-sizing: border-box; line-height: 28px;">

  1. 使用共享變量的方式
    在這種方式中,之所以引入共享變量,是因?yàn)樵撟兞靠梢员欢鄠€(gè)執(zhí)行相同任務(wù)的線程用來作為是否中斷的信號(hào),通知中斷線程的執(zhí)行。
public class ThreadFlag extends Thread 

{ 

    public volatile boolean exit = false; 

    public void run() 

    { 

        while (!exit); 

    } 

    public static void main(String[] args) throws Exception 

    { 

        ThreadFlag thread = new ThreadFlag(); 

        thread.start(); 

        sleep(3000); // 主線程延遲3秒 

        thread.exit = true;  // 終止線程thread 

        thread.join(); 

        System.out.println("線程退出!"); 

    } 

} 

在上面代碼中定義了一個(gè)退出標(biāo)志exit,當(dāng)exit為true時(shí),while循環(huán)退出,exit的默認(rèn)值為false。在定義exit時(shí),使用了一個(gè)Java關(guān)鍵字volatile,這個(gè)關(guān)鍵字的目的是使exit同步,也就是說在同一時(shí)刻只能由一個(gè)線程來修改exit的值。

在《Why Are Thread.stop, Thread.suspend,Thread.resume and Runtime.runFinalizersOnExit Deprecated?》中,建議使用如下的方法來停止線程:

  private volatile Thread blinker; 

    public void stop() { 

        blinker = null; 

    } 

    public void run() { 

        Thread thisThread = Thread.currentThread(); 

        while (blinker == thisThread) { 

            try { 

                thisThread.sleep(interval); 

            } catch (InterruptedException e){ 

            } 

            repaint(); 

        } 

    }

2. 使用interrupt方法終止線程

如果一個(gè)線程由于等待某些事件的發(fā)生而被阻塞,又該怎樣停止該線程呢?這種情況經(jīng)常會(huì)發(fā)生,比如當(dāng)一個(gè)線程由于需要等候鍵盤輸入而被阻塞,或者調(diào)用Thread.join()方法,或者Thread.sleep()方法,在網(wǎng)絡(luò)中調(diào)用ServerSocket.accept()方法,或者調(diào)用了DatagramSocket.receive()方法時(shí),都有可能導(dǎo)致線程阻塞,使線程處于處于不可運(yùn)行狀態(tài)時(shí),即使主程序中將該線程的共享變量設(shè)置為true,但該線程此時(shí)根本無法檢查循環(huán)標(biāo)志,當(dāng)然也就無法立即中斷。這里我們給出的建議是,不要使用stop()方法,而是使用Thread提供的interrupt()方法,因?yàn)樵摲椒m然不會(huì)中斷一個(gè)正在運(yùn)行的線程,但是它可以使一個(gè)被阻塞的線程拋出一個(gè)中斷異常,從而使線程提前結(jié)束阻塞狀態(tài),退出堵塞代碼。

class MyThread extends Thread {

volatile boolean stop = false;

public void run() {

while (!stop) {

System.out.println(getName() + " is running");

try {

sleep(1000);

} catch (InterruptedException e) {

System.out.println("week up from blcok...");

stop = true; // 在異常處理代碼中修改共享變量的狀態(tài)

}

}

System.out.println(getName() + " is exiting...");

}

}

class InterruptThreadDemo3 {

public static void main(String[] args) throws InterruptedException {

MyThread m1 = new MyThread();

System.out.println("Starting thread...");

m1.start();

Thread.sleep(3000);

System.out.println("Interrupt thread...: " + m1.getName());

m1.stop = true; // 設(shè)置共享變量為true

m1.interrupt(); // 阻塞時(shí)退出阻塞狀態(tài)

Thread.sleep(3000); // 主線程休眠3秒以便觀察線程m1的中斷情況

System.out.println("Stopping application...");

}

}

注意:在Thread類中有兩個(gè)方法可以判斷線程是否通過interrupt方法被終止。一個(gè)是靜態(tài)的方法interrupted(),一個(gè)是非靜態(tài)的方法isInterrupted(),這兩個(gè)方法的區(qū)別是interrupted用來判斷當(dāng)前線是否被中斷,而isInterrupted可以用來判斷其他線程是否被中斷。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容