This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Threads and Synchronization and the fly likes group.interrupt not setting the interrupt flag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "group.interrupt not setting the interrupt flag" Watch "group.interrupt not setting the interrupt flag" New topic
Author

group.interrupt not setting the interrupt flag

Vaibhav Jaiswal
Greenhorn

Joined: Jan 17, 2008
Posts: 12
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Vaibhav Jaiswal
Greenhorn

Joined: Jan 17, 2008
Posts: 12
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: group.interrupt not setting the interrupt flag