java 为什么使用 Thread.currentThread().isInterrupted() 而不是 isInterrupted()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11682955/
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
Why use Thread.currentThread().isInterrupted() instead of isInterrupted()?
提问by BumbleGee
I have a question regarding the implementation of a cancellation policy for a Thread subclass. It seems to be common practice to do it like this:
我有一个关于 Thread 子类的取消策略实现的问题。这样做似乎是常见的做法:
class A extends Thread {
[...]
public final void run() {
try {
while(!Thread.currentThread().isInterrupted()) {
[...]
}
} catch (InterruptedException consumed) {
}
}
public final void cancel() {
interrupt();
}
}
The question I have is regarding Thread.currentThread()... Why is it common practice to use currentThread() for checking the interruption flag but not for setting it in the cancel() method? Wouldn't it suffice to just call the isInterrupted() method of A like this:
我的问题是关于 Thread.currentThread()... 为什么使用 currentThread() 来检查中断标志而不是在 cancel() 方法中设置它是常见的做法?像这样调用 A 的 isInterrupted() 方法是不够的:
while (!isInterrupted()) {
[...]
}
I couldn't find an answer neither in the Thread JavaDoc, Brian Goetz' excellent book on concurrent Java or stackoverflow.
我在 Thread JavaDoc、Brian Goetz 关于并发 Java 或 stackoverflow 的优秀书籍中都找不到答案。
Thanks in advance for your insights!
预先感谢您的见解!
Cheers, Georg
干杯,乔治
回答by Andrey Borisov
In your case it is sufficient to just call !isInterrupted()
because you're extending from the Thread
class. Typically you don't extend from Thread
- that's why you call Thread.currentThread()
.
在您的情况下,只需调用就足够了,!isInterrupted()
因为您是从Thread
类扩展而来的。通常,您不会扩展自Thread
- 这就是您调用Thread.currentThread()
.
回答by Geek
Starting from Java 5 it is not a good idea to work with Threads directly. You should rather use Executor framework and choose an execution policy depending on your requirements . The instance isInterrupted()
method tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.The inner isInterrupted()
is actually a native method .
从 Java 5 开始,直接使用线程并不是一个好主意。您应该使用 Executor 框架并根据您的要求选择执行策略。实例isInterrupted()
方法测试此线程是否已被中断。线程的中断状态不受此方法的影响。内部isInterrupted()
实际上是一个native方法。
906 public boolean isInterrupted() {
907 return isInterrupted(false);
908 }
While using the executor framework you do not know which thread instance is executing your code currently and hence the convention is to use Thread.currentThread.isInterrupted()
在使用 executor 框架时,您不知道当前哪个线程实例正在执行您的代码,因此约定是使用 Thread.currentThread.isInterrupted()
回答by Richard Sitze
The cancel
is clear: it's effectively directing the call to this.interrupt
; i.e the Thread instance to which the cancel
method is applied.
将cancel
是显而易见的:它有效地指导调用this.interrupt
; 即cancel
应用该方法的 Thread 实例。
As for the run
method, everything you state make sense on the assumption that the run
method was actually called, and only called, by the (expected) instance of class A
. While that's likely a safe assumption, is it universally true? Now if you're going to ask me for a meaningful example of how/when that assumption mightbe violated, not sure I can pull that out of my hat this late at night :-)
至于run
方法,假设该run
方法实际上被调用,并且仅被class A
. 虽然这可能是一个安全的假设,但它是否普遍适用?现在,如果你要问我一个有意义的例子,说明如何/何时可能违反该假设,我不确定我能否在深夜把它从我的帽子里拿出来:-)
Consider a method other than run
. If it's a method on class A
should it assume it's being called by/on the same thread? Maybe it's a method on another class. So it seems likely this is just a best-practice to eliminate potential bugs: your code becomes copy/paste-proof [@Thomas made this insight as well]
考虑除run
. 如果它是一个方法,class A
它是否应该假设它是由/在同一线程上调用的?也许它是另一个类的方法。因此,这似乎只是消除潜在错误的最佳实践:您的代码成为防复制/防粘贴的 [@Thomas 也提出了这一见解]
回答by Alpedar
It should be same, IF that code is is in currently running thread. Maybe authors fear, that someone will execute run method in other thread, maybe they want it look same as awerywhere else. Or they copy pasted it and did not think.
它应该是相同的,如果该代码在当前正在运行的线程中。也许作者担心有人会在其他线程中执行 run 方法,也许他们希望它看起来和其他地方一样。或者他们复制粘贴它并没有想到。