谁打扰了我的话题?
我了解 InterruptedException 的作用以及抛出它的原因。然而,在我的应用程序中,当我在只有我的应用程序知道的线程上等待 SwingUtilities.invokeAndWait()
时,我得到了它,并且我的应用程序从不调用 Thread.interrupt()
在任何线程上,它也不会将该线程的引用传递给任何人。
所以我的问题是:谁中断了我的线程?
有什么办法可以告诉吗? InterruptedException 不包含请求中断的线程的名称是否有原因?
我读到它可能是执行此操作的框架或库,我们使用以下内容,但我想不出他们中断我的线程的原因:
- Hibernate
- Spring
- Log4J
- Mysql 连接器
I understand what an InterruptedException does and why it is thrown. However in my application I get it when waiting for SwingUtilities.invokeAndWait()
on a thread that is only known by my application, and my application never calls Thread.interrupt()
on any thread, also it never passes the reference of the thread on to anyone.
So my question is: Who interrupts my thread?
Is there any way to tell? Is there a reason why the InterruptedException doesn't contain the name of the Thread that requests the interrupt?
I read that it could be a framework or library that does this, we use the following, but I can't think of reason for them to interrupt my thread:
- Hibernate
- Spring
- Log4J
- Mysql connector
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果可能,您可以扩展
Thread
并覆盖该线程的interrupt()
方法,以打印堆栈跟踪或引发不受支持的操作异常。您还可以使用扩展的 Thread 类来存储对中断线程的引用,并在捕获中断的异常后读取它。
If possible, you could extend
Thread
and overwrite theinterrupt()
method for this thread to print a stacktrace or throw an unsupported operation exception.You could also use the extended
Thread
class to store a reference to the interrupting thread and read it once you catch the interrupted exception.一般来说,如果您想知道谁在做某事,请附加一个调试器,放置一个断点,然后就可以了。无需猜测是否可以重现它!
在这种情况下,您可以在 Thread.interrupt() 处放置断点。如果还有其他线程也被中断(因此您在断点上出现“误报”),您可以添加断点条件(大多数 IDE 允许您轻松执行此操作),例如通过检查线程的名称。
In general, if you want to know who is doing something, attach a debugger, put a breakpoint, and there you go. No need for guessing if you can reproduce it!
In this case, you can put a breakpoint at
Thread.interrupt()
. If there are other threads that are being interrupted too (so you have "false positive" hits on the breakpoint), you could add a breakpoint condition (most IDE's allow you to do that easily), for example by checking the name of the thread.这里有一些奇怪的事情。
来自 invokeAndWait,抛出
InterruptedException
您是否尝试过查看 EDT 中执行的代码是否发送任何异常?或者执行 EDT 中的代码尝试修改该线程的一些变量(我知道这个术语在 java 语言中不是“正统”,但我希望你明白我的意思:任何暗示同步、等待、加入等的代码)
There is something strange here.
From the javadoc of invokeAndWait, an
InterruptedException
is thrownHave you tried to see if the code executed in the EDT sends any exception ? Or do the code in that EDT tries to modify some of this thread's variables (I know this term is not "orthodox" in java language, but I hope you see what I mean : any code implying synchronized, wait, join, ...