- java.lang.Object
-
- ch.bind.philib.concurrent.SingleFlight
-
public class SingleFlight extends Object
SingleFlight implements call deduplication for equal keys. Example:public Result expensiveOperation(final Parameters parameters) throws Exception { return singleFlight.execute(parameters, new Callable<Result>() { @Override public Result call() { return expensiveOperationImpl(parameters); } }); } private Result expensiveOperationImpl(Parameters parameters) { // the real implementation }
-
-
Constructor Summary
Constructors Constructor Description SingleFlight()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <V> Vexecute(Object key, Callable<V> callable)Execute aCallableif no other calls for the samekeyare currently running.
-
-
-
Method Detail
-
execute
public <V> V execute(Object key, Callable<V> callable) throws Exception
Execute aCallableif no other calls for the samekeyare currently running. Concurrent calls for the samekeyresult in one caller invoking theCallableand sharing the result with the other callers. The result of an invocation is not cached, only concurrent calls share the same result.- Parameters:
key- A unique identification of the method call. Thekeymust be uniquely identifiable by it'sObject.hashCode()andObject.equals(Object)methods.callable- TheCallablewhere the result can be obtained from.- Returns:
- The result of invoking the
Callable. - Throws:
Exception- TheExceptionwhich was thrown by theCallable. Alternatively aInterruptedExceptioncan be thrown if the executingThreadwas interrupted while waiting for the result.
-
-