Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interrupt, Interrupted and isInterrupted

 
Kaur Manpreet
Ranch Hand
Posts: 30
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic