wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interrupt, Interrupted and isInterrupted Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interrupt, Interrupted and isInterrupted" Watch "Interrupt, Interrupted and isInterrupted" New topic
Author

Interrupt, Interrupted and isInterrupted

Kaur Manpreet
Ranch Hand

Joined: Feb 14, 2012
Posts: 30

Hi All,

I need some help to understand the output of the code below:




As such the code compiles and runs normally as if none of the interrupt methods were ever there.
1. If we comment only Line 1 then both the threads run simultaneously and they complete normally.
2. If we comment only Line 2 then, both Thread A and B would throw interrupted exception. Thread A when sleep method is invoked and Thread B when join method is invoked.
3. If we comment only Line 3 then both the threads run simultaneously and they complete normally.
4. If we comment Line 1 and Line 2 both the threads run simultaneously and they complete normally.
5. If we comment Line 1 and Line 3 both the threads run simultaneously and they complete normally.
6. If we comment Line 2 and Line 3 both Thread A and B would throw interrupted exception. Thread A when sleep method is invoked and Thread B when join method is invoked.

These are the results I have arrived during my testing on this piece of code.
On this forum I searched on interrupt methods and found that we can assume that interrupt method sets a flag that the thread has been interrupted and any method (wait, join or sleep) that blocks the thread on which interrupt method is invoked would result in InterruptedException. This helps in explaining the result 6 above. But I am not sure of the rest.

Please advise.

Thanks
Kaur Manpreet
Ranch Hand

Joined: Feb 14, 2012
Posts: 30

Thanks all for taking the time,

I was going through the API and I found the following

1. interrupt method set the interrupted status of the thread on which this method is invoked
2. the status is cleared if on the same thread wait, join or sleep are invoked and it receives an InterruptedException
3. interrupted method verifies whether the thread is interrupted and after that it reset the interrupted status of the thread on which interrupt was invoked (I checked this by printing the value of interrupted method). So second invocation returns false.
4. isInterrupted method verifies whether the thread has been interrupted but does not clear the status of the interrupted thread.

wow... its sometimes good to read the API thoroughly

Bharath Ayyagari Mylavarapu
Greenhorn

Joined: Oct 08, 2012
Posts: 1
Hello Manpreet,

The following points mentioned by you are highly helpful:
1. interrupt method set the interrupted status of the thread on which this method is invoked
2. the status is cleared if on the same thread wait, join or sleep are invoked and it receives an InterruptedException
3. interrupted method verifies whether the thread is interrupted and after that it reset the interrupted status of the thread on which interrupt was invoked (I checked this by printing the value of interrupted method). So second invocation returns false.
4. isInterrupted method verifies whether the thread has been interrupted but does not clear the status of the interrupted thread.

However, I have a question related to the same topic, can you please help?

Let us say I have the following 2 classes:



In this case, like you mentioned in your 4 points,
1) Yes, the interrupt method sets the interrupted status of the thread on which this method is invoked. In this case, I believe it is interruptee1.
2. Yes, the status is cleared if on the same thread wait, join or sleep are invoked and it receives an InterruptedException. This is justified because the run() method inside the Interruptee class is is only doing the task of sleep() and when sleep() is interrupted, it throws an InterruptedException.
3) Yes, interrupted method verifies whether the thread is interrupted and after that it reset the interrupted status of the thread on which interrupt was invoked (I checked this by printing the value of interrupted method). So second invocation returns false.

However, point 4) is not clear to me -
4. isInterrupted method verifies whether the thread has been interrupted but does not clear the status of the interrupted thread.

Inside the Interrupter class, I'm trying to invoke this-> System.out.println(t.getName() + " 's interrupted status after interruption is: " + t.isInterrupted());
My expectation is that t.isInterrupted() has to print true since the thread got interrupted (by virtue of the sleep() method getting interrupted and throwing an InterruptedException) but in real, I see that the output is false!! According to point 4) that you mentioned, isInterrupted() does NOT clear the status of the interrupted thread right? Then, why is the result of this method false (instead of true)?

Thanks,
A M Bharth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interrupt, Interrupted and isInterrupted
 
Similar Threads
interrupt method of Thread
Q on thread at Jxam
Synchronization practice: why does thread1 rush through loop without pause or reset?
Interrupt Confusion
join() method; guarenteed behaviour