Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to worry about InterruptedExceptions if I don't interrupt anything myself?

People also ask

Why InterruptedException should not be ignored?

InterruptedExceptions should never be ignored in the code, and simply logging the exception counts in this case as "ignoring". The throwing of the InterruptedException clears the interrupted state of the Thread, so if the exception is not handled properly the information that the thread was interrupted will be lost.

What throws InterruptedException?

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.

How InterruptedException works?

InterruptedException happens when a thread waits or sleeps, and other threads are interrupted and cannot proceed further. We can handle this exception by either using proper try-catch blocks or by avoiding the usage of the sleep () method.

What does thread currentThread () interrupt () do?

By calling Thread. currentThread(). interrupt() , you set the interrupt flag of the thread, so higher-level interrupt handlers will notice it and can handle it appropriately.


Yes, you need to worry about InterruptedException, just as you need to worry for any other checked exception which you must either throw or handle.

Most of the times an InterruptedException signals a stop request, most likely due to the fact that the thread which was running your code was interrupted.

In your particular situation of a connection pool awaiting to aquire a connection, I would say that this is a cancellation issue and you need to abort the aquisition, cleanup, and restore the interrupted flag (see below).


As an example, if you're using some sort of Runnable/Callable running inside an Executor then you need to handle the InterruptedException properly:

executor.execute(new Runnable() {

    public void run() {
         while (true) {
              try {
                 Thread.sleep(1000);
              } catch ( InterruptedException e) {
                  continue; //blah
              }
              pingRemoteServer();
         }
    }
});

This would mean that your task never obeys the interruption mechanism used by the executor and does not allow proper cancellation/shutdown.

Instead, the proper idiom is to restore the interrupted status and then stop execution:

executor.execute(new Runnable() {

    public void run() {
         while (true) {
              try {
                 Thread.sleep(1000);
              } catch ( InterruptedException e) {
                  Thread.currentThread().interrupt(); // restore interrupted status
                  break;
              }
              pingRemoteServer();
         }
    }
});

Useful resources:

  • Shutting down threads cleanly (Java Specialists)
  • Dealing with InterruptedException (Brian Goetz)

Nope. InterruptedException is only generated if you interrupt your thread yourself. If you do not yourself use Thread.interrupt() then I would either re-throw it as some sort of "unexpected exception" or log it as an error and move on. For instance in my code when I am forced to catch InterruptedException and I never call interrupt() myself, I do the equivalent of

catch (InterruptedException exception) {
    throw new RuntimeException("Unexpected interrupt", exception);
}

That's if it is unexpected. There are plenty of places where I deliberately interrupt my threads and in those cases I handle the InterruptedExceptions in a well-defined way. Usually that's by exiting whatever loop I'm in, cleaning up, and then stopping the thread.


Threads can be interrupted by calling Thread.interrupt(). It is used for gracefully signalling a thread that it should do something else. Usually it causes blocking operations (e.g. Thread.sleep()) to return earlier and throw the InterruptedException. If the thread gets interrupted, a flag gets set on it. This flag can be queried via Thread.isInterrupted() call.

If you don't use thread interruption and still get this exception, you could just exit your thread (and preferrably log the exception).

In general, it depends on what your multi-threaded application does.


If you don't know how to handle it in a method I suggest you declare it in the method with throws InterruptedException (and it caller etc)

If it something you never expect to occur I would catch it and wrap it in an AssertionError.