声纳报告以下问题:
Multithreaded correctness - Method does not release lock on all
exception paths findbugs : UL_UNRELEASED_LOCK_EXCEPTION_PATH
This method acquires a JSR-166 (java.util.concurrent) lock, but does not release it on all exception paths out of the method. In general, the correct idiom for using a JSR-166 lock is:
Lock l = ...;
l.lock();
try {
// do something
} finally {
l.unlock();
}
在此代码段上:
public synchronized void put(T element) throws PreconditionException {
Precondition.notNull(element, "Nullobject is not allowed");
lock.lock();
try {
buffer[writeIndex++] = element;
if (writeIndex >= capacity) {
writeIndex = 0;
}
if (size.get() >= capacity) {
// buffer is full
readIndex++;
lastOverflow.set(System.currentTimeMillis());
if (readIndex >= capacity) {
readIndex = 0;
}
return;
}
size.incrementAndGet();
} finally {
try {
notEmpty.signal();
} catch (Exception e) {
e.printStackTrace();
}
lock.unlock();
}
}
我不会得到它,出于什么原因,这不安全?