I have some code that looks like this:
executorService.submit(new Runnable() {
@Override public void run() {
while (!Thread.currentThread().isInterrupted()) {
doWork();
}
}
});
This code is sometimes failing to exit the while loop because the interrupt flag is being cleared somewhere inside doWork(). This method makes calls to third party libraries, one of which is swallowing the interrupt.
Firstly, are there any specific techniques I could use to diagnose this? Secondly, are there any static analysis tools that would be able to spot this sort of bug?
You could extend
Threadand override theinterrupt()method to set your own internal interrupt flag. Then you could compare your internal flag with the thread state after ‘doWork()` finishes to see if they differ.You could always interrupt the thread yourself and see if the interrupt flag is cleared when it exits from
doWork();. This is obviously going to screw up your logic.None that I know of. One of the profilers might be smart enough to catch thread interrupts.