Class StripedExecutor<Key>


  • public class StripedExecutor<Key>
    extends java.lang.Object
    Executor that serializes runnables with the same key, but may parallelize over different keys.
    Author:
    jonmv
    • Constructor Summary

      Constructors 
      Constructor Description
      StripedExecutor()
      Creates a new StripedExecutor which delegates to a Executors.newCachedThreadPool(ThreadFactory).
      StripedExecutor​(java.util.concurrent.ExecutorService executor)
      Creates a new StripedExecutor which delegates to the given executor service.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void execute​(Key key, java.lang.Runnable command)
      Executes the given command.
      void shutdownAndWait()
      Shuts down the delegate executor and waits for it to terminate.
      void shutdownAndWait​(java.time.Duration grace, java.time.Duration die)
      Shuts down the delegate executor and waits for the given grace duration for it to terminate.
      • Methods inherited from class java.lang.Object

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

      • StripedExecutor

        public StripedExecutor()
        Creates a new StripedExecutor which delegates to a Executors.newCachedThreadPool(ThreadFactory).
      • StripedExecutor

        public StripedExecutor​(java.util.concurrent.ExecutorService executor)
        Creates a new StripedExecutor which delegates to the given executor service.
    • Method Detail

      • execute

        public void execute​(Key key,
                            java.lang.Runnable command)
        Executes the given command. If other commands are already running or queued for the given key, execution of this command happens after those, on the same thread as is running them.

        Any exception thrown by the command will only be logged, to allow subsequent commands to run.

      • shutdownAndWait

        public void shutdownAndWait()
        Shuts down the delegate executor and waits for it to terminate.
      • shutdownAndWait

        public void shutdownAndWait​(java.time.Duration grace,
                                    java.time.Duration die)
        Shuts down the delegate executor and waits for the given grace duration for it to terminate. If this fails, tells the executor to ExecutorService.shutdownNow()), and waits for the die duration.