面試官:如何終止線程?有幾種方式?

在 Java 中有以下 3 種方法可以終止正在運行的線程:

  1. 使用退出標誌,使線程正常退出,也就是當 run() 方法完成後線程終止;
  2. 使用 stop() 方法強行終止線程,但是不推薦使用這個方法,因爲使用此方法不安全,目前該方法已被棄用;
  3. 使用 interrupt()方法中斷線程。

第一種:使用標誌位終止線程
使用標誌位終止線程就是定義一個boolean型的標誌位 ,在線程的run方法中根據這個標誌位是爲true還是爲false來判斷是否終止,這種情況多用於while循環中。

class StopThread extends Thread {
    //標誌位
	private boolean flag = true;

	@Override
	public synchronized void run() {
		while (flag) {
			System.out.println(Thread.currentThread().getName()+"---我是子線程");
		}
	}

	/**
	 * @methodDesc: 功能描述:(終止線程)
	 */
	public void stopThread() {
		flag = false;
		System.out.println(getName()+"線程被終止掉了");
	}
}

/**
 @classDesc: 功能描述:(演示終止線程效果)
 */
public class StopThreadDemo {

	public static void main(String[] args) {
		StopThread stopThread1 = new StopThread();
		StopThread stopThread2 = new StopThread();
		stopThread1.start();
		stopThread2.start();

		for (int i = 0; i < 50; i++) {
			System.out.println("------我是主線程-----"+i);
			if(i==30) {
				stopThread1.stopThread();
				stopThread2.stopThread();
		    }
		}
	}
}

第二種: 使用 stop() 終止線程(不安全)
雖然使用stop() 方法可以停止一個正在運行的線程,但是這個方法是不安全的,而且該方法已被棄用。
棄用stop()方法的原因:

  1. 調用 stop() 方法會立刻停止 run() 方法中剩餘的全部任務,包括在 catch 或 finally 語句中的,並拋出ThreadDeath異常,因此可能會導致任務執行失敗。
  2. 調用 stop() 方法會立即釋放該線程所持有的所有的鎖,導致數據得不到同步,出現數據不一致的問題。拿銀行轉賬作爲例子,從A賬戶向B賬戶轉賬100元,這一過程分爲三步,第一步是從A賬戶中減去100元,假如這時線程就被stop了,那麼這個線程就會釋放它所取得鎖,然後其他的線程在獲取到它所釋放的鎖以後就會繼續執行,這樣A賬戶就少了100元,而B賬戶也沒有收到這100元,就會導致數據不一致。這就是stop方法的不安全性。

第三種: 使用interrupt方法中斷線程
使用 interrupt() 方法中斷線程時並不會立即終止線程,而是通知目標線程,告訴它有人希望你終止。至於目標線程收到通知後會如何處理,則完全由目標線程自行決定。理解這一點很重要,如果中斷後,線程立即無條件退出,那麼就會和 stop() 方法沒有任何區別,會導致線程不安全問題。

首先我們先來看一個使用 interrupt() 中斷線程的例子:

public class InterruptThread extends Thread{
	
	@Override
    public void run() {
        for(int i = 0; i <= 10000; i++) {
            System.out.println("i=" + i);
        }
    }
	
    public static void main(String[] args) {
        try {
            InterruptThread interruptThread = new InterruptThread();
            interruptThread.start();
            Thread.sleep(100);
            interruptThread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } 
}

運行結果如下:
在這裏插入圖片描述
從輸出的結果可以發現調用 interrupt 方法並沒有停止掉線程interruptThread中的處理邏輯,也就是說即使線程被設置爲了中斷狀態,但是這個中斷並沒有起到任何作用,那麼怎樣才能停止線程呢?

這就需要使用到 isInterrupted() 方法了:

public boolean Thread.isInterrupted() //判斷是否被中斷

所以如果希望線程在中斷後停止,就應該先判斷線程是否被中斷,然後在執行中斷處理代碼:

public class InterruptThread extends Thread{
	
	@Override
    public void run() {
        for(int i = 0; i <= 10000; i++) {
        	//判斷是否被中斷
            if(Thread.currentThread().isInterrupted()){
                //處理中斷
                break;
            }
            System.out.println("i=" + i);
        }
    }
	
    public static void main(String[] args) {
        try {
            InterruptThread interruptThread = new InterruptThread();
            interruptThread.start();
            Thread.sleep(100);
            interruptThread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } 
}

運行結果:
在這裏插入圖片描述
在上面這段代碼中,我們增加了 Thread.isInterrupted() 來判斷當前線程是否被中斷了,如果線程被中斷,則退出 for 循環,結束線程。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章