You are here:

Java/InterruptedException in multi threading


Hii sir!
My doubt is on InterruptedException in java .
I am asking my doubt based on sleep method in java.
When we call  interrupt  on a thread which is in sleep state then InterruptedException is thrown.
Below  are my doubts:
1)Imagine that  we have called  interrupt method on a thread ,which  has come out from a sleep.
Then will that thread throws InterruptedException?
2)Imagine that we have called interrupt method on a thread ,which will go to sleep.
That means now the thread is not in sleep state.
Then will that thread throws Interrupted exception?

1. If the thread is not sleeping or performing an interruptable task (like file I/O), then it will continue to run.
2. Yes, when the thread calls sleep, it will immediately result in an InterruptedException.

What happens is that if a thread is not performing an interrupted when interrupted, then the interrupted flag for the thread will be set. This flag is cleared by sleep and other methods which result in an InterruptedException. If you want to check if the current running thread has been interrupted, then you can use boolean interrupted = Thread.interrupted() which determines if there is an interrupted flag and clears it. Typically if you are performing a long task that doesn't involve file IO or using sleep, then you would have code like:

while(!taskIsComplete) {
 if(Thread.interrupted()) {
    throw new InterruptedException();
 //Rest of code for this loop iteration here.


All Answers

Answers by Expert:

Ask Experts


Artemus Harper


I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.


I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Washington State University

MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2017 All rights reserved.