简体   繁体   English

谁在抛出InterruptedException?

[英]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()并且t1Runnable 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.

相关问题 wait() 总是抛出 InterruptedException - wait() is always throwing InterruptedException Java wait()不会抛出InterruptedException - Java wait() not Throwing InterruptedException FutureTask中的awaitDone抛出InterruptedException - awaitDone in FutureTask throwing InterruptedException ThreadPoolExecutor.shutdownNow() 不在线程中抛出 InterruptedException - ThreadPoolExecutor.shutdownNow() not throwing InterruptedException in Thread 从主线程休眠是抛出InterruptedException - sleep from main thread is throwing InterruptedException 条件的类的await方法不抛出InterruptedException - await method of class Condition not throwing InterruptedException 调用interrupt()时,线程未引发InterruptedException - Thread not throwing an InterruptedException when interrupt() is called 为什么Java.lang.Process不会在此代码中引发InterruptedException? - Why is Java.lang.Process not throwing InterruptedException in this code? 如果线程在调用Object.wait()之前被中断,该线程甚至会在抛出InterruptedException之前费心地释放锁。 - If a thread is interrupted before calling Object.wait(), will the thread even bother to release the lock before throwing InterruptedException 在停止线程时抛出InterruptedException是否必不可少? 我可以使用其他异常类型吗? - Is throwing InterruptedException essential when stopping threads? Can I use another exception type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM