Re: Findbugs and locks?
- From: Lew <com.lewscanon@lew>
- Date: Wed, 30 Jul 2008 09:17:03 -0400
Jeff Higgins wrote:
if (n < lockArray.length) {
ReentrantReadWriteLock rrwl = lockArray[n];
WriteLock lock = rrwl.writeLock();
try {
lock.lock();
// do some disk I/O
} finally {
lock.unlock();
}
}
}
Is there a chance that lock.lock() would fail?
If so, would lock.unlock() cause any harm?
The usual pattern for resource acquisition and release with try-finally is
resource.acquire();
// break flow on acquire() failure
try
{
...
}
finally
{
resource.release();
}
That way the release() only occurs if the acquire() succeeds. Moving the acquire() into the try{} means that the release() will occur even if acquire() fails. This is how Knute coded it, and I don't see why his way caused a warning.
I would be more comfortable if Knute's way didn't cause any warnings, because I don't understand it either. However, I'm unfamiliar with Findbugs (although many recommend it very highly).
--
Lew
.
- Follow-Ups:
- Re: Findbugs and locks?
- From: Jeff Higgins
- Re: Findbugs and locks?
- References:
- Findbugs and locks?
- From: Knute Johnson
- Re: Findbugs and locks?
- From: Jeff Higgins
- Findbugs and locks?
- Prev by Date: Re: Findbugs and locks?
- Next by Date: Re: Findbugs and locks?
- Previous by thread: Re: Findbugs and locks?
- Next by thread: Re: Findbugs and locks?
- Index(es):