java 检查线程是否被中断?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16324525/
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
Check if a thread is interrupted?
提问by Junaid Hassan
I just want to get know about if this thread is interrupting or not if I'm doing it right? please give me hint if I'm wrong
我只是想知道这个线程是否正在中断,如果我做得对吗?如果我错了,请给我提示
public void run(){
int i;
while(!Thread.currentThread().isInterrupted()){
for(i=1;i<=100;i++){
System.out.println("THREAD VALUE AFTER 1 SECOND IS: "+i);
if(i==3){
Thread.currentThread().interrupt();
gotoInform();
break;
}
try{
Thread.currentThread().sleep(1000);////to sleep the Thread for 1 Second (1000ms)
}
catch(Exception e){
System.out.printf("Error"+e);
}
}
}
回答by ruakh
This is wrong, because if sleep
finds that the thread is interrupted, it will throw an InterruptedException
and clear the interrupted flag. You then swallow that exception, suppressing any record that the thread was ever interrupted. Instead, you should write something more like this:
这是错误的,因为如果sleep
发现线程被中断,它会抛出一个InterruptedException
并清除中断标志。然后你吞下那个异常,抑制线程被中断的任何记录。相反,您应该编写更像这样的内容:
public void run(){
for(int i=1;i<=100;i++){
System.out.println("THREAD VALUE AFTER 1 SECOND IS: "+i);
if(i==3){
Thread.currentThread().interrupt();
gotoInform();
break;
}
try{
Thread.currentThread().sleep(1000);
}
catch(final Exception e){
e.printStackTrace();
if(e instanceof InterruptedException) {
// just in case this Runnable is actually called directly,
// rather than in a new thread, don't want to swallow the
// flag:
Thread.currentThread().interrupt();
}
return;
}
}
}
(Note: I'm assuming that this is not "real" code, but rather, that you're just trying to learn how thread interruption works. In "real" code, you should almost never need to interrupt the current thread in this way.)
(注意:我假设这不是“真正的”代码,而是您只是想了解线程中断是如何工作的。在“真正的”代码中,您几乎不需要在此代码中中断当前线程大大地。)
回答by paulk23
As mentioned before, a thread interrupting itself is pointless (unless used to re-interrupt itself after catching an InterruptedException) . You're basically using the thread's internal interrupted flag as a conditional variable here - which while it may work is not at all what it's supposed to be used for and will be confusing to anyone else who would need to read your code. Use a loop counter instead as suggested above to make the code much cleaner.
如前所述,中断自身的线程毫无意义(除非用于在捕获 InterruptedException 后重新中断自身)。您基本上是在此处使用线程的内部中断标志作为条件变量 - 虽然它可能起作用,但它根本不是它应该用于的用途,并且会让其他需要阅读您的代码的人感到困惑。按照上面的建议使用循环计数器来使代码更清晰。
Also, your statement:
另外,你的声明:
System.out.println("THREAD VALUE AFTER 1 SECOND IS: "+i);
is erroneous as it will execute immediately the first time through the loop (when the time is closer to zero seconds).
是错误的,因为它将在第一次通过循环时立即执行(当时间接近零秒时)。
回答by Gray
I just want to get know about if this thread is interrupting or not if I'm doing it right?
我只是想知道这个线程是否正在中断,如果我做得对吗?
@ruakh is correct that it is alwaysa good idea to re-interrupt a thread once InterruptedException
is thrown.
@ruakh 是正确的,一旦抛出线程就重新中断它总是一个好主意InterruptedException
。
However, if the goal of your code is to self-interrupt and no other threads will interrupt the running thread, then you will never get to the sleep()
call since break;
is called after the thread is interrupted.
但是,如果您的代码的目标是自中断并且没有其他线程会中断正在运行的线程,那么您将永远无法sleep()
调用,因为break;
在线程中断后调用。
If the thread is always just self interrupted then I would just use a flag. Something like:
如果线程总是自我中断,那么我将只使用一个标志。就像是:
boolean done = false;
while (!done) {
...
if(i==3){
done = true;
...
}
}
Even though you interrupt your thread, you then call gotoInform()
which may call wait()
or sleep()
itself and cause an InterruptedException
. You'll need to make sure that code behaves well and re-interrupts the thread if so.
即使您中断了线程,您gotoInform()
也可以调用 which 可能会调用wait()
或sleep()
本身并导致InterruptedException
. 您需要确保代码运行良好,如果是,则重新中断线程。