在 Java 中殺死執行緒

Rupam Yadav 2023年10月12日
  1. 在 Java 中使用 boolean 標誌來殺死或停止一個執行緒
  2. 在 Java 中使用 interrupt() 殺死或停止一個執行緒
在 Java 中殺死執行緒

Java 中的執行緒允許我們並行執行多個任務,從而實現多工處理。我們可以使用 Thread 類在 Java 中建立一個執行緒。在本文中,我們將介紹兩種殺死執行緒的方法。

雖然執行緒在完成所有任務後會被 Thread 類的 run() 方法銷燬,但有時我們可能會想線上程完全執行完之前就殺死或停止它。

在 Java 中使用 boolean 標誌來殺死或停止一個執行緒

要顯式地殺死一個執行緒,我們可以使用一個布林標誌來通知執行緒何時停止任務。下圖中,兩個執行緒列印一行其名稱,然後兩個執行緒都休眠約 100 毫秒。執行緒一直執行到布林標誌 exitThread 變為 true 為止。

thread1thread2 是兩個建立的執行緒,在這兩個執行緒中都傳遞了一個名字作為引數。由於 ExampleThread 類的建構函式中有 thread.start(),開始執行一個執行緒,所以兩個執行緒都會執行。我們可以看到,由於兩個執行緒是並行執行的,所以輸出會隨機列印兩個執行緒的名稱。

如果要停止執行緒,我們將呼叫 stopThread(),這是 ExampleThread 中的一個方法,將 exitThread 設定為 true,執行緒最終停止是因為 while(!exitThread) 變成了 false

public class KillThread {
  public static void main(String[] args) {
    ExampleThread thread1 = new ExampleThread("Thread One");
    ExampleThread thread2 = new ExampleThread("Thread Two");

    try {
      Thread.sleep(500);
      thread1.stopThread();
      thread2.stopThread();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("**Exiting the main Thread**");
  }
}

class ExampleThread implements Runnable {
  private String name;
  private boolean exitThread;
  Thread thread;

  ExampleThread(String name) {
    this.name = name;
    thread = new Thread(this, name);
    System.out.println("Created Thread: " + thread);
    exitThread = false;
    thread.start();
  }

  @Override
  public void run() {
    while (!exitThread) {
      System.out.println(name + " is running");
      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    System.out.println(name + " has been Stopped.");
  }

  public void stopThread() {
    exitThread = true;
  }
}

輸出:

Created Thread: Thread[Thread One,5,main]
Created Thread: Thread[Thread Two,5,main]
Thread Two is running
Thread One is running
Thread Two is running
Thread One is running
Thread One is running
Thread Two is running
Thread One is running
Thread Two is running
Thread One is running
Thread Two is running
**Exiting the main Thread**
Thread Two has been Stopped.
Thread One has been Stopped.

在 Java 中使用 interrupt() 殺死或停止一個執行緒

我們在這裡將使用前面的例子,但是用一個新的方法叫做 interrupt()。這個函式在一個執行緒上被呼叫時,會立即停止執行。在這個例子中,我們使用 thread.isInterrupted() 來檢查 interrupt() 是否被呼叫。

為了停止兩個執行緒,我們將呼叫 thread1.thread.interrupt()thread2.thread.thread.interrupt() 導致執行緒的終止。

public class KillThread {
  public static void main(String[] args) {
    ExampleThread thread1 = new ExampleThread("Thread One");
    ExampleThread thread2 = new ExampleThread("Thread Two");

    try {
      Thread.sleep(6);
      thread1.thread.interrupt();
      thread2.thread.interrupt();
      Thread.sleep(8);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    System.out.println("**Exiting the main Thread**");
  }
}

class ExampleThread implements Runnable {
  private String name;
  Thread thread;

  ExampleThread(String name) {
    this.name = name;

    thread = new Thread(this, name);
    System.out.println("Created Thread: " + thread);

    thread.start();
  }

  @Override
  public void run() {
    while (!thread.isInterrupted()) {
      System.out.println(name + " is running");
    }

    System.out.println(name + " has been Stopped.");
  }
}

輸出:

Created Thread: Thread[Thread One,5,main]
Created Thread: Thread[Thread Two,5,main]
Thread One is running
Thread Two is running
Thread One has been Stopped.
Thread Two has been Stopped.
**Exiting the main Thread**
作者: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

相關文章 - Java Thread