Re: InterruptedException handling
- From: Daniel Pitts <newsgroup.spamfilter@xxxxxxxxxxxxxxxxxxx>
- Date: Sun, 06 Sep 2009 16:18:18 -0700
Kenneth P. Turvey wrote:
I used to do a lot of programming in C (still do sometimes) and when a call was made to sleep there were a number of reasons that it might return early that had little to do with the program being written. It was typical to wrap these calls in a loop so that the program would sleep for the correct amount of time. So when I first moved to Java I would typically do the same thing with an InterruptedException, catching it and looping back to the wait or the sleep or whatever had caused the exception.The loop is necessary for Object.wait() calls, but InterruptedException is a different beast.
Or, if you are expecting it as a specific type of signaling device (such as "abort your current work")
I think this was a mistake. In Java it seems that the only reason a thread would be interrupted is do to a direct call to Thread.interrupt
(). So it seems that the best way to handle this exception if it is unexpected is to let it bubble up through the code like one usually would.
Only if you are the owner of the Thread Lifecycle (eg. you have a contract with the part of the code that is likely to call Thread.interrupt())
Of course if the code is designed to do something with an InterruptedException, handling it makes sense, but should it simply be ignored in code that doesn't expect this to happen? This line of reasoning also extends to similar exceptions like the BrokenBarrier exception and others.
Is there any reason to handle InterruptedExceptions as a special case?
For example, the exception happens in a Callable that is passed to a ThreadPoolExecutor, you should probably let it propagate. However, if you are implementing ThreadPoolExecutor, your worker threads may have special handling for that exception.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
.
- References:
- InterruptedException handling
- From: Kenneth P. Turvey
- InterruptedException handling
- Prev by Date: Re: Making String variable and value available for all Classes
- Next by Date: Re: My EL not worky
- Previous by thread: Re: InterruptedException handling
- Next by thread: ♀¤♀¤♀AAAAA handbags
- Index(es):
Relevant Pages
|