34

I'm relatively new to Threading in Java and I've noticed that everytime I use Thread.sleep() I have to catch InterrupetdException.

What kind of behaviour causes this, and in simple applications where I have a monitor thread can I just Ignore the exception?

Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
  • Similar to this question: http://stackoverflow.com/questions/17494717/why-a-thread-would-interrupt-another-thread/17495107#17495107 – Eric des Courtis Jul 08 '13 at 15:56

7 Answers7

30

It happens when something calls interrupt() on the thread. This article by Brian Goetz explains the interruption mechanism and how you should handle InterruptedExceptions:

"The most common response to InterruptedException is to swallow it -- catch it and do nothing (or perhaps log it, which isn't any better) -- as we'll see later in Listing 4. Unfortunately, this approach throws away important information about the fact that an interrupt occurred, which could compromise the application's ability to cancel activities or shut down in a timely manner."

"If you catch InterruptedException but cannot rethrow it, you should preserve evidence that the interruption occurred [...]. This task is accomplished by calling interrupt() to "reinterrupt" the current thread."

Community
  • 1
  • 1
Dan Dyer
  • 53,737
  • 19
  • 129
  • 165
  • 2
    Thank you for bringing up this point. It's a PITA to have to manage InterruptedExceptions, but it's important. – Spencer Kormos Oct 22 '08 at 17:22
  • 3
    Great reference, but doesn't quite answer the OP's real question. Reinterpreted, it's "why would a thread get interrupted?", and the given answers are "because some other thread decided to". I'm guessing the OP's real intent was "is there something in the larger Java ecosystem that would cause this?".E.g. System.exit? Return from a main() method? Sunspots? :-) – Charles Roth Mar 19 '19 at 16:52
8

As others have said, it is caused by some other thread calling interrupt() on the Thread object that is sleeping.

What this means in plain english, is that some other thread has decided to cancel the sleeping thread. The try/catch block is there so you can gracefully handle the cancellation of the thread, and safely clean up any resources, or shut down whatever operation it was doing correctly.

If you don't actually need to do any of that, then yes, you still need an empty catch block. But that's Java for you...​​​​​​​​​​​​​​​​​​​​

Eric des Courtis
  • 5,135
  • 6
  • 24
  • 37
madlep
  • 47,370
  • 7
  • 42
  • 53
7

Some advices from Java Concurrency in Practice:

  • Propagate the exception (possibly after some task-specific cleanup), making your method an interruptible blocking method, too; or
  • Restore the interruption status so that code higher up on the call stack can deal with it.
  • Only code that implements a thread's interruption policy may swallow an interruption request. General-purpose task and library code should never swallow interruption requests.
jassuncao
  • 4,695
  • 3
  • 30
  • 35
3

The primary case is when someone calls Thread.interrupt() on your thread.

It may be safer to throw a RuntimeException if it happens when you're really not expecting it, but for very simple cases you can probably ignore it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

From the javadocs:

Class InterruptedException

Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.

Hope that answers your question.

Miguel Ping
  • 18,082
  • 23
  • 88
  • 136
-1

Well if some other Thread calls thread.interupt(), while the thread is sleeping, you'll get the Exception. And yes, you can probably just put try..catch arround the sleep() and ignore it ;)

Jan Gressmann
  • 5,481
  • 4
  • 32
  • 26
  • 25
    Downgraded because you should *never* ignore any exception, this leads to undocumented behaviors and bugs. Either log it, or bubble it up. – Spencer Kormos Oct 22 '08 at 17:21
  • Swallowing an InterruptedException compromises an application's ability to cancel activities or shut down in a timely information.More information here http://www.ibm.com/developerworks/library/j-jtp05236/ – Kumar Abhinav Aug 06 '14 at 10:44
  • 3
    While one definitely should not ignore most exceptions. This is a prime example of an exception that may be ignored in some cases. For example when the reference to the Thread only exists in scope then there's no unexpected behavior possible. We are Software Engineers and supposed to think for ourselves. If you don't even ask yourself why it's best practice to handle exceptions, you probably are a bad engineer. – Eldar Kersebaum Jun 28 '17 at 12:03
-1

InterruptedException is a checked exception so unfortunately you cannot just ignore it. In most simple cases you do not have to do anything in the catch clause because you are sure that it will not happen.

From the API

Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192