[prev in list] [next in list] [prev in thread] [next in thread] 

List:       rhq-devel
Subject:    On Proper Handling of Interrupts in Java Code
From:       ccrouch () redhat ! com (Charles Crouch)
Date:       2011-08-30 16:06:48
Message-ID: 843199124.437446.1314720408064.JavaMail.root () zmail05 ! collab ! prod ! int ! phx2 ! redhat ! com
[Download RAW message or body]

Lets get a BZ raised to track addressing this.

Thanks
Charles

----- Original Message -----
> Hi Folks,
> 
> I was looking at a support case when I noticed this sort of code...
> 
> try {
> this.lock.lockInterruptibly();
> } catch (InterruptedException e) {
> throw new RuntimeException(e);
> }
> 
> The lockInterruptibly() API used above throws an InterruptedException
> IF
> the thread is interrupted while acquiring the lock OR IF the thread is
> currently interrupted; the latter is perhaps the most common case, and
> the former typically only occurring during shut-down. The catch block
> fails to restore the current threads interrupt status (it eats the
> interrupt). Failing to properly restore a threads interrupt status can
> lead to unanticipated application behaviour, and occasional (extremely
> difficult to diagnose) instability.
> 
> Could we please follow standard techniques for handling interrupts?
> 
> Five years ago, back in 2006, IBM wrote a decent article on
> interrupts,
> it's a pretty good read; it is especially useful before writing APIs
> so
> that from day one they are interrupt safe. I found the document very
> well written and the guidelines helpful to me in creating other
> products
> since 2007. Here is the link:
> 
> http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html
> 
> A quick scan across the code base reveals:
> 
> * (0) occurances of Thread.currentThread().interrupt();
> * (94) occurances of catch-InterruptedException
> 
> Rather than eating interrupts, it is better to set the interrupt
> status,
> as the following rewritten code does:
> 
> try {
> this.lock.lockInterruptibly();
> } catch (InterruptedException e) {
> // lets callers service interrupts!
> Thread.currentThread().interrupt();
> throw new RuntimeException(e);
> }
> 
> Cheers,
> 
> -Bob
> 
> 
> _______________________________________________
> rhq-devel mailing list
> rhq-devel at lists.fedorahosted.org
> https://fedorahosted.org/mailman/listinfo/rhq-devel

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic