[英]Who is throwing the InterruptedException?
In the following code, who is throwing the InterruptedException
caught at line-(K+1)? 在以下代码中,谁引发了第(K + 1)行捕获的InterruptedException
?
public class Den implements Runnable {
Object T = new Object();
public void m1() {
System.out.println("here .. ");
synchronized(T){
try {
T.wait(); // line-K
} catch (InterruptedException ex) { // line-(K+1)
System.out.println("An InterruptedException is thrown ");
ex.printStackTrace();
}
System.out.println("here 2");
}
} // m1()
@Override
public void run() {m1();}
public static void main (String ...jsdf) {
Den H = new Den();
Thread t1 = new Thread(H);
t1.start();
t1.interrupt(); // line-W
}
}
The output of this code is 此代码的输出是
run:
here ..
An InterruptedException is thrown
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
here 2
at java.lang.Object.wait(Object.java:502)
at prj.Dene.m1(Dene.java:12)
at prj.Dene.run(Dene.java:22)
at java.lang.Thread.run(Thread.java:745)
BUILD SUCCESSFUL (total time: 0 seconds)
The stack trace tells that it's the T.wait()
method that is throwing it-- as it should be, there's nothing else in the code that throws an InterruptedException
. 堆栈跟踪表明正在抛出该T.wait()
的是T.wait()
方法-应该如此,代码中没有其他东西抛出InterruptedException
。
But then-- interrupt()
is called on the thread t1
and t1
is executing the Runnable H
as its Thread.target
object. 但是然后-在线程t1
上调用了interrupt()
并且t1
将Runnable H
作为其Thread.target
对象执行。 How come T
is receiving that interrupt? T
如何收到该中断? Is the interrupt()
on Thread.target
invoking in turn the interrupt()
-s of objects it is waiting on? Thread.target
上的interrupt()
是否Thread.target
调用它正在等待的对象的interrupt()
s? Thread.interrupt()
is invoking a native Thread.interrupt0()
-- can't see these. Thread.interrupt()
正在调用本机Thread.interrupt0()
-无法看到这些。
TIA. TIA。
t1.interrupt();
is obviously the initiation of the interrupt. 显然是中断的开始。 You may be unaware, but this only "schedules" an interrupt; 您可能没有意识到,但这仅“安排”了一个中断。 the target thread (t1) is not necessarily immediately disturbed... InterruptedException only occurs at well defined points so that you can handled this in code. 目标线程(t1)不一定会立即受到干扰... InterruptedException仅在定义明确的点发生,以便您可以在代码中进行处理。
wait()
is one of these well defined points (along w/ sleep()
, and some I/O operations). wait()
是这些定义明确的点之一(带有sleep()
和一些I / O操作)。
In your case, the system injects an exception inside the wait()
call. 在您的情况下,系统会在wait()
调用中注入异常。 Don't put toooooo much into the line number reported. 不要在所报告的行号中添加太多内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.