- Why does a sleep thread need a try catch to catch Interrupted Exception?
- Why does a sleep even emit an Interrupted Exception error? This are the two questions I really wanna find out about in java programming I've been searching through google and i've still haven't found a clear explanation is to why this two things happen.

- 10,265
- 1
- 33
- 41

- 137
- 1
- 2
- 9
-
Folks stumbling onto this from search engines who have more questions now about what `InterruptedException` is really for should check out this insightful answer: https://stackoverflow.com/a/3976377/1858327 – Captain Man Nov 03 '22 at 16:56
6 Answers
An InterruptedException
is thrown when the thread is blocked/waiting and it is interrupted by another thread (by means of Thread.interrupt
). Think of it as a request for immediate termination, that do not suffer the drawbacks of Thread.stop()
.
This way, even if you instruct a thread to sleep for several years, you are able to interrupt that thread.
The recommended practice is aborting whatever you are processing when a InterruptedException
is thrown.

- 6,997
- 6
- 48
- 74

- 12,100
- 5
- 46
- 57
-
Oh so, for example i have two frames, frame1 and frame2. Frame1 has a button that shows frame 2. By declaring an interrupted exception, I can click that button to show my frame2 while frame1 is sleeping? – Jericho Aganon Mar 20 '13 at 15:47
-
Sorry, I don't have enough experience with concurrency in AWT/Swing. I use threads for background tasks. If you can rephrase your question not using GUI I would be happy to answer. – Javier Mar 20 '13 at 15:53
-
Funny thing is I got interested in java for its gui and i've only been studying for a week now and I think that was a bad idea to start on since i have to know the basics first(Everything non gui related). Although I hope this is a good example, i'll sleep a statement that adds two numbers and the one thing i've noticed about sleeping is that it "freezes" yung program or does that only happen in gui? (sorry I really don't know). And by adding an interrupted exception, you're free to do any other stuff you want to do while the statement your ordered to sleep is sleeping? – Jericho Aganon Mar 20 '13 at 15:57
-
Sleep "freezes" the **current** thread. If you want to do another task, you should perform it in another thread. – Javier Mar 20 '13 at 16:01
- Because a Thread cant complete its normal execution if you Interrupt it, and you need to catch that in order to be prepared to do something.
- Because a thread waiting is different from an interrupted thread, a thread waiting can be resumed, but an interrupted thread is already finish execution.
-
So by declaring an interrupted exception, I can freely click any buttons i've inputted while the stuff i put to sleep is sleeping? Cause that's what i've understood so far – Jericho Aganon Mar 20 '13 at 15:50
-
check this out http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#interrupt%28%29 – jsedano Mar 20 '13 at 15:53
Sleep and interrupts are not related semantically. It's just that Java designers thought that when you want your thread to sleep, it's a good opportunity to remind you about interrupts. This is like Duke saying "It looks like you're trying to sleep, would you also like to make your thread a good citizen by making sure that it responds to interrupt events properly, when the need for a way to get it to terminate abruptly at a later stage in your project arises?"
So one will often see code like this:
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
//Don't worry about it.
}
Sometimes people say this is considered bad practice. But if you're not planning to use the interrupts facility in your program, then these exceptions will never be thrown, so you have to ask yourself whether doing the extra work in order to take care of these exceptions, in case you decide to add the interruptibility feature to your program some time later, makes sense. This is one of those things that Java designers insisted that every thread should do — that you could interrupt()
it, and it will quickly and cleanly abort what it's doing. I think that's unnecessary in many cases, but people will look at your code, see this and still say "eew, bad practice!"
The official Java tutorial explains interrupts. Basically, if you have one thread t
doing some processing, and then the user wants to cancel it, from another thread you would call t.interrupt()
. In the code that's running on thread t
, whenever it sleep()
s, or wait()
s, etc., an InterruptedException
will be thrown. If it doesn't do any of these, then it can (should) also find out if it had been interrupted using Thread.interrupted()
from time to time. In all these ways of finding out about interrupts, it should abandon what it's doing and clean up ASAP. (That is to say: if it does so, then this behaviour might be useful to you or someone — that's the idea of interrupts.)
So, Java makes this a checked exception of the sleep(..)
method, to force you to think about using this facility. The other part of the rationale is that if sleep(..)
is interrupted, then it will wake up early, and that's an exceptional event. (But remember, there's that "if".)
The important thing is that interrupts don't just happen for no reason. They happen if you write code to make them happen, or if someone else does, who launches your threads and has a need to cancel their activities. So this is who causes Thread.sleep(..)
to throw an InterruptedException
. You do. And if you don't, then you still need to catch it.
Edit. By the way, it would be a better practice to do it this way:
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
throw new UnsupportedOperationException("Interrupts not supported.", ie);
}
So, if you, or somebody else, ever tries to interrupt this thread by mistake later, then when they go to test it, they will be reminded that this feature is not implemented. (UnsupportedOperationException
is a subclass of RuntimeException
, so it's unchecked.)

- 22,495
- 17
- 107
- 124
When you ask a thread to sleep, the expected behavior for that thread is to sleep for that much time. So if the sleep is interrupted, it will throw InterruptedException to indicate that it couldn't complete the task. And you might want to take care of what should be done if it is Interrupted.

- 3,794
- 2
- 36
- 47
-
If i put an interrupted exception via catch try, thread sleep won't "freeze" my program while doing its sleep thing? and i'm able to do other tasks such as clicking buttons from my program while my the statement i put to sleep is sleeping? – Jericho Aganon Mar 20 '13 at 15:52
-
Each time you click a button, a new thread will be created and it will call actionPerformed method with an ActionEvent as argument. – vishnu viswanath Mar 20 '13 at 16:07
-
-
http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html?page=3 check this out. it might help you – vishnu viswanath Mar 20 '13 at 16:11
-
i've put it in a class that will be run when my "if(button == a.getsource)" statement is satisfied. Its purpose is that i wanna set a label to visible then after sleeping set another one to visible while being able to press other buttons aswell while that class is doing its thing. – Jericho Aganon Mar 20 '13 at 16:13
-
So your problem is that your UI get stuck for that much time when the thread is sleeping, and you want to avoid that? – vishnu viswanath Mar 20 '13 at 16:16
-
Yeah, without the interrupted exception it freezes, after i've put in the frame seems to be responding well and i can even click my buttons. I'm just wondering if that's what the interrupted exception try catch do – Jericho Aganon Mar 20 '13 at 16:20
-
I am not sure. you have to either throw InterruptedException or Catch it. How did you do without either? – vishnu viswanath Mar 20 '13 at 16:34
-
i've catched it, its catch(InterruptedException e) to be exact. – Jericho Aganon Mar 20 '13 at 16:42
There is a clean example of how the Interrupted Exception can be thrown here : http://www.javamex.com/tutorials/threads/thread_interruption.shtml
And a discuss about sleep()
and yield()
here : http://www.coderanch.com/t/508657/threads/java/Sleep-Yield-state

- 1,184
- 7
- 26
- 57
-
The thread can be accessed from outside in a concurrent manner while it is working or asleep (that's all the fun !) : then it can be asked to exit "badly", that's why the exception is raised. – Benj Mar 20 '13 at 15:55
-
1Oh so its like your locked in a room with a sleeping guy, by adding an interrupted exception, you get to unlock the room and do other stuff "or" have a bucket of water in your hand and "wake" the guy by stopping it to sleep? Sorry I make real life situations to apply to these problems to help me understand them better. – Jericho Aganon Mar 20 '13 at 16:05
-
-
Then you can consider using the "up" button, or the "check" button to say thanks to every guy who helped ;) – Benj Mar 22 '13 at 15:34
-
I've instantly clicked up as soon as I saw the answer but it said I need 15 reputaion points. – Jericho Aganon Mar 24 '13 at 02:18
-
1
It's because sleep()
could potentially block forever/a long time so you need a way to be able to cancel this behaviour. Because it is not "normal" completion when you interrupt the action you may need to perform some specific compensating or remedial action, e.g., to send an alert that you never received a notification, clean up resources, etc.
There is a pretty good developer works article on the subject.

- 1,541
- 12
- 25
-
So by adding an interrupted exception, I can freely close my program while sleeping or do something else while it sleeps? – Jericho Aganon Mar 20 '13 at 16:00
-
More like if you want to close your application then your sleeping thread is interrupted and has a chance to clear up it's business before shut down, e.g., write state to a db, log a message. – James Mar 20 '13 at 16:30