Class PredictableExecutor

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

    public class PredictableExecutor
    extends java.util.concurrent.AbstractExecutorService
    implements java.util.concurrent.ExecutorService
    (Somewhat) predictable ExecutorService.

    ExecutorService which behaves similar to the one created by Executors.newFixedThreadPool(int, ThreadFactory), but assigns command to specific thread based on it's PredictableExecutor.PickyTask.hint(), Object.hashCode(), or hint value explicitly specified when the command was passed to this PredictableExecutor.

    • Constructor Detail

      • PredictableExecutor

        public PredictableExecutor​(int buckets,
                                   java.util.concurrent.ThreadFactory threadFactory)
        Creates PredictableExecutor instance.
        Parameters:
        buckets - number of buckets or 0 to match available processors
        threadFactory - ThreadFactory to use to create threads
      • PredictableExecutor

        public PredictableExecutor​(int buckets,
                                   java.util.concurrent.ThreadFactory threadFactory,
                                   boolean directExec)
        Creates PredictableExecutor instance. Meant for testing purposes.
        Parameters:
        buckets - number of buckets or 0 to match available processors
        threadFactory - ThreadFactory to use to create threads
        directExec - direct executors
      • PredictableExecutor

        public PredictableExecutor​(java.util.concurrent.ThreadFactory threadFactory)
        Creates PredictableExecutor instance with bucket size set to number of available processors.
        Parameters:
        threadFactory - ThreadFactory to use to create threads
    • Method Detail

      • newPredictableExecutor

        public static PredictableExecutor newPredictableExecutor​(int buckets,
                                                                 java.util.concurrent.ThreadFactory threadFactory)
        Creates PredictableExecutor instance.
        Parameters:
        buckets - number of buckets or 0 to match available processors
        threadFactory - ThreadFactory to use to create threads
        Returns:
        PredictableExecutor
      • backendExecutorService

        protected java.util.concurrent.ExecutorService backendExecutorService​(java.util.concurrent.ThreadFactory threadFactory,
                                                                              boolean direct)
        Creates a single thread ExecutorService to use in the backend.
        Parameters:
        threadFactory - ThreadFactory to use to create threads
        direct - direct executors
        Returns:
        single thread ExecutorService or direct executor
      • execute

        public void execute​(java.lang.Runnable command,
                            int hint)
        Executes given command at some time in the future.
        Parameters:
        command - the Runnable task
        hint - value to pick thread to run on.
      • execute

        public void execute​(java.lang.Runnable command,
                            java.util.function.Function<java.lang.Runnable,​java.lang.Integer> hintFunction)
        Executes given command at some time in the future.
        Parameters:
        command - the Runnable task
        hintFunction - Function to compute hint value
      • execute

        public void execute​(java.lang.Runnable command)
        Specified by:
        execute in interface java.util.concurrent.Executor
      • shutdown

        public void shutdown()
        Specified by:
        shutdown in interface java.util.concurrent.ExecutorService
      • shutdownNow

        public java.util.List<java.lang.Runnable> shutdownNow()
        Specified by:
        shutdownNow in interface java.util.concurrent.ExecutorService
      • isShutdown

        public boolean isShutdown()
        Specified by:
        isShutdown in interface java.util.concurrent.ExecutorService
      • isTerminated

        public boolean isTerminated()
        Specified by:
        isTerminated in interface java.util.concurrent.ExecutorService
      • awaitTermination

        public boolean awaitTermination​(long timeout,
                                        java.util.concurrent.TimeUnit unit)
                                 throws java.lang.InterruptedException

        Note: It'll try, but is not assured that the method will return by specified timeout.

        Specified by:
        awaitTermination in interface java.util.concurrent.ExecutorService
        Throws:
        java.lang.InterruptedException
      • newTaskFor

        protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor​(java.util.concurrent.Callable<T> callable)
        Overrides:
        newTaskFor in class java.util.concurrent.AbstractExecutorService
      • newTaskFor

        protected <T> PredictableExecutor.PickyFutureTask<T> newTaskFor​(java.lang.Runnable runnable,
                                                                        T value)
        Overrides:
        newTaskFor in class java.util.concurrent.AbstractExecutorService