如何从 Java 中的另一个线程杀死一个线程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1680525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:35:12  来源:igfitidea点击:

How do I kill a thread from another thread in Java?

javamultithreading

提问by Raji

I am calling two threads from a main thread, call them thread 1 and thread 2. When thread 1 stops, I want to stop or kill thread 2 also. How can I do this?There is a change in actual output what i want.That is There is a main class which is also thread.From main class i am calling thread1 and thread2.I am giving an input to thread1 from main class but when this input is getting changed i want to kill the running thread1 and start it once again with another input.The second thread,thread2 will run with the output given by the thread1.So eventually when the first thread is killed the second will be running but will give an output only if t6here is an input for that thread.

我从主线程调用两个线程,分别称为线程 1 和线程 2。当线程 1 停止时,我也想停止或终止线程 2。我该怎么做?我想要的实际输出发生了变化。那是有一个主类,它也是线程。从主类我正在调用线程1和线程2。我从主类向线程1提供输入,但是当这个输入被改变时,我想杀死正在运行的线程 1 并用另一个输入再次启动它。第二个线程,线程 2 将使用线程 1 给出的输出运行。所以最终当第一个线程被杀死时,第二个线程将运行但仅当 t6here 是该线程的输入时才会给出输出。

回答by abyx

Java has deprecated methods for explicitly killing another thread (like Thread.stop / Thread.destroy). The right way is to make sure the operations on the other thread can handle being told to stop (for example, they expect an InterruptedException, which means you can call Thread.interrupt() in order to stop it).

Java 已弃用显式终止另一个线程的方法(如 Thread.stop / Thread.destroy)。正确的方法是确保另一个线程上的操作可以处理被告知停止的操作(例如,他们期望一个 InterruptedException,这意味着您可以调用 Thread.interrupt() 来停止它)。

You might also be interested in setting the second thread as a daemon thread, which means that in case all other threads in the VM have finished then the process will exit.

您可能还对将第二个线程设置为守护线程感兴趣,这意味着如果 VM 中的所有其他线程都已完成,则该进程将退出。

回答by Brian Agnew

See this Java Specialist articleon how to shut down threads cleanly.

请参阅这篇关于如何干净地关闭线程的Java 专家文章

Briefly, the article recommends Thread.interrupt()plus the appropriate InterruptedExceptionhandling in the thread code. That's an interesting discussion in itself, and something I rarely see done correctly.

简而言之,文章建议在线程代码中Thread.interrupt()加上适当的InterruptedException处理。这本身就是一个有趣的讨论,而且我很少看到正确完成的事情。

回答by Erich Kitzmueller

According to these instructions, it's best to use flag variables to tell the other thread to stop its work cleanly.

根据这些说明,最好使用标志变量来告诉其他线程干净地停止其工作。

回答by rsp

In the run()method of your thread, check an attribute that is set through an accessor method like shutdown()when you want to stop the thread, use that value to end the while loop. For example:

run()线程的方法中,检查通过访问器方法设置的属性,例如shutdown()当您想要停止线程时,使用该值结束 while 循环。例如:

boolean stopping = false;

Thread currentThread = null;

public void run() {

    currentThread = Thread.currentThread();

    while (!isStopping()) {

        // do something, sleep a while
    }
}

public synchronized void shutdown() {

    stopping = true;

    currentThread.notifyAll();
}

public synchronized boolean isStopping() {

    return stopping;
}