线程应该如何在 Java 中关闭自己?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2491588/
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
How a thread should close itself in Java?
提问by Roman
This is a short question. At some point my thread understand that it should suicide. What is the best way to do it:
这是一个简短的问题。在某些时候,我的线程明白它应该自杀。最好的方法是什么:
- Thread.currentThread().interrupt();
- return;
- Thread.currentThread().interrupt();
- 返回;
By the way, why in the first case we need to use currentThread
? Is Thread
does not refer to the current thread?
顺便说一句,为什么在第一种情况下我们需要使用currentThread
?是Thread
不是指当前线程?
采纳答案by Enno Shioji
If you want to terminate the thread, then just returning is fine. You do NOT need to call Thread.currentThread().interrupt()
(it will not do anything bad though. It's just that you don't need to.) This is because interrupt()
is basically used to notify the owner of the thread (well, not 100% accurate, but sort of). Because you are the owner of the thread, and you decided to terminate the thread, there is no one to notify, so you don't need to call it.
如果您想终止线程,那么只需返回即可。你不需要调用Thread.currentThread().interrupt()
(虽然它不会做任何坏事。只是你不需要。)这是因为interrupt()
基本上用于通知线程的所有者(嗯,不是 100% 准确,但有点)。因为你是线程的所有者,你决定终止线程,没有人通知,所以你不需要调用它。
By the way, why in the first case we need to use currentThread? Is Thread does not refer to the current thread?
顺便说一句,为什么在第一种情况下我们需要使用 currentThread?是不是Thread不是指当前线程?
Yes, it doesn't. I guess it can be confusing because e.g. Thread.sleep() affects the current thread, but Thread.sleep() is a static method.
是的,它没有。我想这可能会令人困惑,因为例如 Thread.sleep() 影响当前线程,但 Thread.sleep() 是一个静态方法。
If you are NOT the owner of the thread (e.g. if you have not extended Thread
and coded a Runnable
etc.) you should do
如果您不是线程的所有者(例如,如果您没有扩展Thread
和编码Runnable
等),您应该这样做
Thread.currentThread().interrupt();
return;
This way, whatever code that called your runnable will know the thread is interrupted = (normally) should stop whatever it is doing and terminate. As I said earlier, it is just a mechanism of communication though. The owner might simply ignore the interrupted status and do nothing.. but if you do set the interrupted status, somebody might thank you for that in the future.
这样,任何调用 runnable 的代码都会知道线程被中断 =(通常)应该停止它正在执行的任何操作并终止。正如我之前所说,它只是一种交流机制。所有者可能会简单地忽略中断状态并且什么也不做……但是如果您确实设置了中断状态,那么将来有人可能会为此感谢您。
For the same reason, you should never do
出于同样的原因,你永远不应该这样做
Catch(InterruptedException ie){
//ignore
}
Because if you do, you are stopping the message there. Instead one should do
因为如果你这样做,你就会在那里停止消息。相反,应该做
Catch(InterruptedException ie){
Thread.currentThread().interrupt();//preserve the message
return;//Stop doing whatever I am doing and terminate
}
回答by Jon Skeet
If you're at the top level - or able to cleanly getto the top level - of the thread, then just returning is nice. Throwing an exception isn't as clean, as you need to be able to check that nothing's going to catch the exception and ignore it.
如果您处于线程的顶级 - 或者能够干净地到达顶级 - ,那么返回就很好。抛出异常并不那么干净,因为您需要能够检查没有任何东西会捕获异常并忽略它。
The reason you need to use Thread.currentThread()
in order to call interrupt()
is that interrupt()
is an instance method - you need to call it on the thread you want to interrupt, which in your case happens to be the current thread. Note that the interruption will only be noticed the next time the thread would block (e.g. for IO or for a monitor) anyway - it doesn't mean the exception is thrown immediately.
您需要使用Thread.currentThread()
才能调用的原因interrupt()
是这interrupt()
是一个实例方法 - 您需要在要中断的线程上调用它,在您的情况下恰好是当前线程。请注意,中断只会在下次线程阻塞(例如,对于 IO 或监视器)时才会被注意到 - 这并不意味着立即抛出异常。
回答by Marcelo Cantos
Thread is a class, not an instance; currentThread() is a static method that returns the Thread instance corresponding to the calling thread.
线程是一个类,而不是一个实例;currentThread() 是一个静态方法,它返回调用线程对应的 Thread 实例。
Use (2). interrupt() is a bit brutal for normal use.
使用 (2)。interrupt() 对于正常使用来说有点残酷。
回答by Martijn Courteaux
If the run method ends, the thread will end.
如果 run 方法结束,则线程将结束。
If you use a loop, a proper way is like following:
如果使用循环,正确的方法如下:
// In your imlemented Runnable class:
private volatile boolean running = true;
public void run()
{
while (running)
{
...
}
}
public void stopRunning()
{
running = false;
}
Of course returning is the best way.
当然回国是最好的办法。
回答by phil294
If you simply call interrupt()
, the thread will not automatically be closed. Instead, the Thread might even continueliving, if isInterrupted()
is implemented accordingly. The only way to guaranteedly closea thread, as asked for by OP, is
如果您只是调用interrupt()
,线程不会自动关闭。相反,如果相应地实现,线程甚至可能继续存在isInterrupted()
。按照OP 的要求,保证关闭线程的唯一方法是
Thread.currentThread().stop();
Method is deprecated, however.
然而,方法已被弃用。
Calling return
only returns from the current method. This only terminates the thread if you're at its top level.
调用return
仅从当前方法返回。如果您处于线程的顶层,这只会终止线程。
Nevertheless, you should work with interrupt()
and build your code around it.
不过,您应该使用它interrupt()
并围绕它构建代码。