在 Java 中杀死线程

Rupam Yadav 2023年1月30日 2020年12月31日
  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**
Author: 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