Re: Findbugs and locks?



Jeff Higgins wrote:
Knute Johnson wrote:
Findbugs gives the warning "Method does not release lock on all exception paths" on a method like the one below. Could it be because the lock is from an array of locks and it can't determine which? Or is it because you could put code outside of the try/finally block that could leave without unlocking the lock? Any other ideas? It can't leave the method without unlocking can it?


Produces no bug reports using FindBugs 1.3.2.20080222
in Eclipse 3.3

import java.io.IOException;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

public class SSCCE {

ReentrantReadWriteLock lockArray[] =
new ReentrantReadWriteLock[5];

void method(int n) throws IOException {

if (n < lockArray.length) {
ReentrantReadWriteLock rrwl = lockArray[n];
WriteLock lock = rrwl.writeLock();
try {
lock.lock();
// do some disk I/O
} finally {
lock.unlock();
}
}
}

public static void main(String[] args) {

}

}

Putting the lock() call inside the try/finally block does stop findbugs from complaining. I put it on the outside because that is the way that Goetz showed in his book, Java Concurrency In Practice. He does mention that you must consider what happens if an exception is thrown outside of the try block. I suppose findbugs complains about my code is it is possible to throw an exception between the lock and the try even though I have no code there.

I don't know for sure what the ramifications are of putting the lock inside the try block but I can't think of any at the moment.

Thanks,

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
.



Relevant Pages

  • Re: CMutex /CEvent (multiple threads)
    ... deals with exception detection. ...  If your function does not handle an exception in Java, ... designer did not understand what a Mutex was and that the notion that Lock could return ... ...roll back transaction ...
    (microsoft.public.vc.mfc)
  • =?iso-8859-1?q?D=E9j=E0_vu_bug:_The_Matrix_uses_the_.NET_Framework!?=
    ... encounters a .NET Framework bug. ... intermittent lock leak that was not the kind you find in text book. ... exception is thrown in either A, ... private static void Throw ...
    (microsoft.public.dotnet.framework)
  • Re: Access problems at MERSTHAM
    ... they have also changed the "lock". ... Alan, AKA The Bogeyman, Aka Alan. ... Posted via NewsDemon.com - Premium Uncensored Newsgroup Service ... Unlimited Access, Anonymous Accounts, Uncensored Broadband Access ...
    (uk.rec.subterranea)
  • Re: D2005 Code Folding
    ... > "The thread must call LeaveCriticalSection once for each time that it ... > When EnterCriticalSection doesn't return a result? ... When an exception is raised no lock is granted. ...
    (borland.public.delphi.non-technical)
  • Re: D2005 Code Folding
    ... EnterCriticalSection and LeaveCriticalSection is called in the ... > When an exception is raised no lock is granted. ... > returns immediately regardless of whether the lock was granted. ...
    (borland.public.delphi.non-technical)