Who interrupts my thread?
Asked Answered
R

3

14

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
Rolle answered 19/3, 2010 at 8:58 Comment(5)
I think you mean "Who DARES interrupt my thread?"Exodus
+1 - Are you doing Thread#join ? it throws InterruptedException.Intransigeance
Indeed Scott, I would prefer my threads to be not interruptable (because I don't want to comply to the request anyway). We don't do Thread#join ourselves, but I could imagine that invokeAndWait uses such a mechanism. However, would Thead#join invoke an interrupt on another thread? Also I'm pretty sure that noone waits for my thread to complete (as it is will never).Rolle
did Thread.dumpStacktrace() doesn't help ???Unroof
The current thread isn't the one doing the interrupt, so the dumpStackTrace shouldn't give me the information I need. The stacktrace from the exception shows it happens in Object.wait()Rolle
H
12

If possible, you could extend Thread and overwrite the interrupt() 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.

Henna answered 19/3, 2010 at 12:16 Comment(0)
H
5

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.

Hypothermia answered 19/3, 2010 at 9:47 Comment(0)
S
2

There is something strange here.

From the javadoc of invokeAndWait, an InterruptedException is thrown

if we're interrupted while waiting for the event dispatching thread to finish excecuting doRun.run()

Have 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, ...

Strow answered 19/3, 2010 at 9:14 Comment(1)
If the Runnable we are waiting for throws an exception it should give an InvocationTargetException, which isn't the case. It is possible that our threads has a lock while waiting, could that cause it? I'm sure we don't have a deadlock. The thread we wait for creates a dialog that we use in the waiting thread.Rolle

© 2022 - 2024 — McMap. All rights reserved.