Class StripedExecutorService

  • All Implemented Interfaces:
    java.util.concurrent.Executor, java.util.concurrent.ExecutorService

    public class StripedExecutorService
    extends java.util.concurrent.AbstractExecutorService
    The StripedExecutorService accepts Runnable/Callable objects that also implement the StripedObject interface. It executes all the tasks for a single "stripe" consecutively.

    In this version, submitted tasks do not necessarily have to implement the StripedObject interface. If they do not, then they will simply be passed onto the wrapped ExecutorService directly.

    Idea inspired by Glenn McGregor on the Concurrency-interest mailing list and using the SerialExecutor presented in the Executor interface's JavaDocs.

    http://cs.oswego.edu/mailman/listinfo/concurrency-interest

    • Constructor Summary

      Constructors 
      Constructor Description
      StripedExecutorService()
      This constructs a StripedExecutorService that wraps a cached thread pool.
      StripedExecutorService​(int numberOfThreads)
      This constructs a StripedExecutorService that wraps a fixed thread pool with the given number of threads.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean awaitTermination​(long timeout, java.util.concurrent.TimeUnit unit)
      Returns true if the wrapped ExecutorService terminates within the allotted amount of time.
      void execute​(java.lang.Runnable command)
      Executes the command.
      boolean isShutdown()
      Returns true if shutdown() or shutdownNow() have been called; false otherwise.
      boolean isTerminated()
      Returns true if this pool has been terminated, that is, all the SerialExecutors are empty and the wrapped ExecutorService has been terminated.
      protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor​(java.lang.Runnable runnable, T value)
      If the runnable also implements StripedObject, we store the stripe object in a thread local, since the actual runnable will be wrapped with a FutureTask.
      protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor​(java.util.concurrent.Callable<T> callable)
      If the callable also implements StripedObject, we store the stripe object in a thread local, since the actual callable will be wrapped with a FutureTask.
      void shutdown()
      Shuts down the StripedExecutorService.
      java.util.List<java.lang.Runnable> shutdownNow()
      All the tasks in each of the SerialExecutors are drained to a list, as well as the tasks inside the wrapped ExecutorService.
      java.util.concurrent.Future<?> submit​(java.lang.Runnable task)
      Delegates the call to submit(task, null).
      <T> java.util.concurrent.Future<T> submit​(java.lang.Runnable task, T result)
      If the task is a StripedObject, we execute it in-order by its stripe, otherwise we submit it directly to the wrapped executor.
      <T> java.util.concurrent.Future<T> submit​(java.util.concurrent.Callable<T> task)
      If the task is a StripedObject, we execute it in-order by its stripe, otherwise we submit it directly to the wrapped executor.
      java.lang.String toString()
      Prints information about current state of this executor, the wrapped executor and the serial executors.
      • Methods inherited from class java.util.concurrent.AbstractExecutorService

        invokeAll, invokeAll, invokeAny, invokeAny
      • Methods inherited from class java.lang.Object

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

      • StripedExecutorService

        public StripedExecutorService()
        This constructs a StripedExecutorService that wraps a cached thread pool.
      • StripedExecutorService

        public StripedExecutorService​(int numberOfThreads)
        This constructs a StripedExecutorService that wraps a fixed thread pool with the given number of threads.
        Parameters:
        numberOfThreads - the number of threads
    • Method Detail

      • newTaskFor

        protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor​(java.lang.Runnable runnable,
                                                                        T value)
        If the runnable also implements StripedObject, we store the stripe object in a thread local, since the actual runnable will be wrapped with a FutureTask.
        Overrides:
        newTaskFor in class java.util.concurrent.AbstractExecutorService
      • newTaskFor

        protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor​(java.util.concurrent.Callable<T> callable)
        If the callable also implements StripedObject, we store the stripe object in a thread local, since the actual callable will be wrapped with a FutureTask.
        Overrides:
        newTaskFor in class java.util.concurrent.AbstractExecutorService
      • submit

        public java.util.concurrent.Future<?> submit​(java.lang.Runnable task)
        Delegates the call to submit(task, null).
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
        Overrides:
        submit in class java.util.concurrent.AbstractExecutorService
      • submit

        public <T> java.util.concurrent.Future<T> submit​(java.lang.Runnable task,
                                                         T result)
        If the task is a StripedObject, we execute it in-order by its stripe, otherwise we submit it directly to the wrapped executor. If the pool is not running, we throw a RejectedExecutionException.
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
        Overrides:
        submit in class java.util.concurrent.AbstractExecutorService
      • submit

        public <T> java.util.concurrent.Future<T> submit​(java.util.concurrent.Callable<T> task)
        If the task is a StripedObject, we execute it in-order by its stripe, otherwise we submit it directly to the wrapped executor. If the pool is not running, we throw a RejectedExecutionException.
        Specified by:
        submit in interface java.util.concurrent.ExecutorService
        Overrides:
        submit in class java.util.concurrent.AbstractExecutorService
      • execute

        public void execute​(java.lang.Runnable command)
        Executes the command. If command implements StripedObject, we execute it with a SerialExecutor. This method can be called directly by clients or it may be called by the AbstractExecutorService's submit() methods. In that case, we check whether the stripes thread local has been set. If it is, we remove it and use it to determine the StripedObject and execute it with a SerialExecutor. If no StripedObject is set, we instead pass the command to the wrapped ExecutorService directly.
      • shutdown

        public void shutdown()
        Shuts down the StripedExecutorService. No more tasks will be submitted. If the map of SerialExecutors is empty, we shut down the wrapped executor.
      • shutdownNow

        public java.util.List<java.lang.Runnable> shutdownNow()
        All the tasks in each of the SerialExecutors are drained to a list, as well as the tasks inside the wrapped ExecutorService. This is then returned to the user. Also, the shutdownNow method of the wrapped executor is called.
      • isShutdown

        public boolean isShutdown()
        Returns true if shutdown() or shutdownNow() have been called; false otherwise.
      • isTerminated

        public boolean isTerminated()
        Returns true if this pool has been terminated, that is, all the SerialExecutors are empty and the wrapped ExecutorService has been terminated.
      • awaitTermination

        public boolean awaitTermination​(long timeout,
                                        java.util.concurrent.TimeUnit unit)
                                 throws java.lang.InterruptedException
        Returns true if the wrapped ExecutorService terminates within the allotted amount of time.
        Throws:
        java.lang.InterruptedException
      • toString

        public java.lang.String toString()
        Prints information about current state of this executor, the wrapped executor and the serial executors.
        Overrides:
        toString in class java.lang.Object