File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes InterruptedException in multi threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "InterruptedException in multi threading" Watch "InterruptedException in multi threading" New topic
Author

InterruptedException in multi threading

shankara me
Greenhorn

Joined: Nov 30, 2012
Posts: 14
Hi !
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 named Thread1,which has come out from a sleep.
Then will Thread1 throws InterruptedException?
2)Imagine that we have called interrupt method on a thread named Thread2,which will go to sleep.
That means now Thread2 is not in sleep state.
Then will Thread2 throws Interrupted exception?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Try it and see.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3934
    
  17

shankara me wrote:Hi !
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 named Thread1,which has come out from a sleep.
Then will Thread1 throws InterruptedException?

That depends on if Thread1 does something else that can be interrupted and throws an InterruptedException. If not, then no, calling interrupt() on a thread will only cause an InterruptedExcetpion if whatever action is being performed reacts to an interrupt with that exception. If Thread1 comes out of sleep() before the interrupt(), and doesn't do anything else which throws the InterruptException, then no InterruptException will be thrown.


2)Imagine that we have called interrupt method on a thread named Thread2,which will go to sleep.
That means now Thread2 is not in sleep state.
Then will Thread2 throws Interrupted exception?

Yes, probably. Calling an interrupt() on a thread sets an 'interrupted' flag. Assuming you don't do anything in Thread2 that resets that flag before calling sleep() then when Thread2 calls sleep() the sleep() method will react like it was interrupted and will throw the InterruptedException.


Steve
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: InterruptedException in multi threading
 
Similar Threads
A few questions on wait(), sleep(int) and InterruptedException
Doubt with thread
Doubt in Thread's interrupt() method
join() method usage
Doubt on Thread Interrupt