Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

group.interrupt not setting the interrupt flag

 
Vaibhav Jaiswal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing strange behavior (not as per spec) In my test application I have to create some number of threads which will do some database transaction. To manage these threads I am using a thread group. to close the threads I have used the Thread.isInterrupted() flag i.e. they check in a loop for this flag and if it is set they come out of the loop. When I want to shut down I call group.interrupt() but to my surprise the loop was still running I put the the trace comments but the isInterrupted flag is not set at all. Moreover this happens once in a while.

Am I missing anything.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any try{} catch (InterruptedException) {} clauses?

If the group.interrupt() occurs when the task is blocking in an interruptable waiting state, it would cause the InterruptedException to be thrown. Usually when a command throws an InterruptedException it clears the interrupt flag. In your catch block you would have to re-set the interrupt to see it's effect in your loop.
 
Vaibhav Jaiswal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am calling the isInterrupted method in case a InterruptedException is thrown, will it make this method also return false.
Actually I am not calling any API that throws InterruptedException but I am calling other APIs which may be calling wait on some objects. I have a try catch block but it is outside the loop so the thread should any way exit in case the called API is throwing exception.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav Jaiswal wrote:I am calling the isInterrupted method in case a InterruptedException is thrown, will it make this method also return false.


Yes. The InterruptedException cleats the interrupted flag. The static interrupted() method and the non-static isInterrupted() method check the same flag.

Actually I am not calling any API that throws InterruptedException but I am calling other APIs which may be calling wait on some objects. I have a try catch block but it is outside the loop so the thread should any way exit in case the called API is throwing exception.


It might be worth running the code through a debugger and checking the state of interruption at various places.

If you think that another API might be consuming your InterruptedException and not passing it on in some manner (which is bad manners for an API intended to be used by others, I think. Are they possibly reporting it wrapped in some other exception?) then you will need to use some other flag other than interruption. For example, set some volatile boolean variable to a true value when you want the loop to run, then set it to false just before doing the group interrupt. Your loop condition would check this new 'go' flag (and maybe the interrupt flag as well).

For example:


You would call the keepRunningTask() method from the condition of your loop.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic