Class MultiKeySequentialProcessor<KeyType>

  • Type Parameters:
    KeyType - Type of the Key.
    All Implemented Interfaces:
    java.lang.AutoCloseable

    @ThreadSafe
    public class MultiKeySequentialProcessor<KeyType>
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Concurrent async processor that allows parallel execution of tasks with different keys, but serializes the execution of tasks with the same key.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <ReturnType>
      java.util.concurrent.CompletableFuture<ReturnType>
      add​(java.util.Collection<KeyType> keys, java.util.function.Supplier<java.util.concurrent.CompletableFuture<? extends ReturnType>> toRun)
      Queues up a new task to execute, subject to the given dependency Keys.
      <ReturnType>
      java.util.concurrent.CompletableFuture<ReturnType>
      addWithFilter​(java.util.function.Predicate<KeyType> keyFilter, java.util.function.Supplier<java.util.concurrent.CompletableFuture<? extends ReturnType>> toRun)
      Queues up a new task to execute, subject to the dependency keys that match the given filter.
      void close()  
      int getCurrentTaskCount()
      Gets the number of concurrent tasks currently executing.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MultiKeySequentialProcessor

        @ConstructorProperties("executor")
        public MultiKeySequentialProcessor​(java.util.concurrent.Executor executor)
    • Method Detail

      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • getCurrentTaskCount

        public int getCurrentTaskCount()
        Gets the number of concurrent tasks currently executing.
        Returns:
        task count
      • add

        public <ReturnType> java.util.concurrent.CompletableFuture<ReturnType> add​(java.util.Collection<KeyType> keys,
                                                                                   java.util.function.Supplier<java.util.concurrent.CompletableFuture<? extends ReturnType>> toRun)
        Queues up a new task to execute, subject to the given dependency Keys. This task will not begin execution until all previous tasks for the given dependency Keys have finished. In addition, no subsequent task for any of the given dependency Keys will begin executing until this task has finished executing.
        Type Parameters:
        ReturnType - Return type.
        Parameters:
        keys - A Collection of KeyType objects representing the Keys that this task is dependent on.
        toRun - A Supplier that will be invoked when it is this task's turn to run. It will return a CompletableFuture that will complete when this task completes.
        Returns:
        A CompletableFuture that will complete with the result from the CompletableFuture returned by toRun, when toRun completes executing.
      • addWithFilter

        public <ReturnType> java.util.concurrent.CompletableFuture<ReturnType> addWithFilter​(java.util.function.Predicate<KeyType> keyFilter,
                                                                                             java.util.function.Supplier<java.util.concurrent.CompletableFuture<? extends ReturnType>> toRun)
        Queues up a new task to execute, subject to the dependency keys that match the given filter. This task will not begin execution until all previous tasks whose keys match the given filter have finished. In addition, no subsequent task for any key that matches the given filter will begin executing until this task has finished executing.
        Type Parameters:
        ReturnType - Return type.
        Parameters:
        keyFilter - A Predicate defining the filter that determines which keys this task will depend on.
        toRun - A Supplier that will be invoked when it is this task's turn to run. It will return a CompletableFuture that will complete when this task completes.
        Returns:
        A CompletableFuture that will complete with the result from the CompletableFuture returned by toRun, when toRun completes executing.