bug5239: extended LockUtil by lock convenience functions forwarding

exceptions
This commit is contained in:
Dennis Aulenbacher committed 2020-08-31 18:33:57 +02:00
1 parent ad8cfa0ce2
commit 1f85008815
1 file changed
+44
@@ -57,6 +57,14 @@ public class LockUtil {
T run();
}
public interface RunnableWithResultAndException<T, E extends Throwable> {
T run() throws E;
}
public interface RunnableWithException<E extends Throwable> {
void run() throws E;
}
private static final int NUMBER_OF_SECONDS_TO_WAIT_FOR_LOCK = 5;
private static final Logger logger = Logger.getLogger(Util.class.getName());
private static final Map<NamedReentrantReadWriteLock, TimePoint> lastTimeWriteLockWasObtained = new ConcurrentWeakHashMap<NamedReentrantReadWriteLock, TimePoint>();
@@ -476,4 +484,40 @@ public class LockUtil {
unlockAfterWrite(lock);
}
}
public static <T, E extends Throwable> T executeWithWriteLockAndResultExpectException(NamedReentrantReadWriteLock lock, RunnableWithResultAndException<T, E> runnable) throws E{
lockForWrite(lock);
try {
return runnable.run();
} finally {
unlockAfterWrite(lock);
}
}
public static <E extends Throwable> void executeWithWriteLockExpectException(NamedReentrantReadWriteLock lock, RunnableWithException<E> runnable) throws E{
lockForWrite(lock);
try {
runnable.run();
} finally {
unlockAfterWrite(lock);
}
}
public static <T, E extends Throwable> T executeWithReadLockAndResultExpectException(NamedReentrantReadWriteLock lock, RunnableWithResultAndException<T, E> runnable) throws E{
lockForRead(lock);
try {
return runnable.run();
} finally {
unlockAfterRead(lock);
}
}
public static <E extends Throwable> void executeWithReadLockExpectException(NamedReentrantReadWriteLock lock, RunnableWithException<E> runnable) throws E{
lockForRead(lock);
try {
runnable.run();
} finally {
unlockAfterRead(lock);
}
}
}