聲納報告以下問題:的Java聲納/ FindBugs的 - 多線程正確性
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();
}
}
我不會得到它,原因是什麼,這是不是安全的?