here I have implemented two following functions like below:
Output1 computeFirst(Input1 input) {
String lockName = input.getId();
LockItem lock
Runnable would not work as there is an output as well.
public interface Identifiable{
String getId();
}
public static <T extends Identifiable,R> R executeWithLock(T input, Function<T,R> func) {
String lockName = input.getId();
LockItem lockItem = acquireLock(lockName);
try{
return func.apply(input);
} catch(Exception e){
log.error(e);
return null;
} finally {
releaseLock(lockItem);
}
}
call like
Output1 output = executeWithLock(input1,
(input) -> {return /*compute output as in critical section*/ null;});