Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who interrupts my thread?

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
like image 862
Thirler Avatar asked Mar 19 '10 08:03

Thirler


People also ask

What can cause a thread to be interrupted?

interrupt() method: If any thread is in sleeping or waiting for a state then using the interrupt() method, we can interrupt the execution of that thread by showing InterruptedException. A thread that is in the sleeping or waiting state can be interrupted with the help of the interrupt() method of Thread class.

What happens when a thread gets interrupted?

An InterruptedException is thrown when a thread is interrupted while it's waiting, sleeping, or otherwise occupied. In other words, some code has called the interrupt() method on our thread. It's a checked exception, and many blocking operations in Java can throw it.

How do you check if a thread is interrupted?

isInterrupted() The interrupted() is a static method in Thread class that determines if the current thread has been interrupted. The isInterrupted() is an instance method that tests if this thread instance has been interrupted. "The interrupted status of the thread is cleared by this method".

Can JVM interrupt a thread?

The JVM itself doesn't call interrupt, it just stops the threads cold if you call System::exit or the user ctrl-c's the app (The threads just stop in the middle of whatever operation they were doing--this is the reason System::exit isn't recommended);


1 Answers

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.

like image 156
josefx Avatar answered Oct 26 '22 17:10

josefx