Class KeyedConcurrencyReducer<K,R>

java.lang.Object
com.aoapps.concurrent.KeyedConcurrencyReducer<K,R>
Direct Known Subclasses:
ConcurrencyLimiter

public class KeyedConcurrencyReducer<K,R> extends Object
Limits the concurrency to a resource identified by any arbitrary key object. When a second thread tries to access the same resource as a previous thread, it will share the results that are obtained by the previous thread.
  • Constructor Details

    • KeyedConcurrencyReducer

      public KeyedConcurrencyReducer()
  • Method Details

    • executeSerialized

      public R executeSerialized(K key, Callable<? extends R> callable) throws InterruptedException, ExecutionException

      Executes a callable at most once for the given key. If a callable is in the process of being executed by a different thread (determined by key, not the callable instance), the current thread will wait and use the results obtained by the other thread.

      Consider the following scenario:

      1. Thread A invokes MySQL: "CHECK TABLE example FAST QUICK"
      2. Thread B invokes MySQL: "CHECK TABLE example FAST QUICK" before Thread A has finished
      3. Thread B wait for results determined by Thread A
      4. Thread A completes, passes results to Thread B
      5. Threads A and B both return the results obtained only by Thread A
      Throws:
      InterruptedException
      ExecutionException