Class UnfairExecutor

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

    public class UnfairExecutor
    extends AbstractSubmitterExecutor
    A very high performance SubmitterExecutor implementation. Though to get those performance gains, some guarantees are reduced. Most prominently is execution order, this scheduler does not ensure that tasks are executed in the order they are submitted, but rather tasks are consumed however is fastest.

    This executor has an execution queue per thread. That way each thread has a many producer, one consumer safety guarantees. Compared to other pools which have to deal with a many to many thread safety issue. Determining the thread queue may be based on a variety of information. A couple built in distribution solutions are UnfairExecutor.TaskHashXorTimeStripeGenerator (default) and UnfairExecutor.AtomicStripeGenerator.

    This scheduler will work best when the following conditions are true. First because a long running task can block other tasks from running (even when other threads are idle). It is best that tasks should be equally sized. We also recommend having thread counts which are prime numbers for a more even hash distribution. It is also important to recognize that it is generally a bad idea to block any of these threads waiting for results from processing that is expected to happen on the same executor.

    Since:
    4.5.0
    • Method Detail

      • isShutdown

        public boolean isShutdown()
        Function to check if the thread pool is currently accepting and handling tasks.
        Returns:
        true if thread pool is running
      • shutdown

        public void shutdown()
        Stops any new tasks from being submitted to the pool. But allows all tasks which are submitted to execute, or scheduled (and have elapsed their delay time) to run. If recurring tasks are present they will also be unable to reschedule. This call will not block to wait for the shutdown of the scheduler to finish. If shutdown() or shutdownNow() has already been called, this will have no effect.

        If you wish to not want to run any queued tasks you should use shutdownNow().

      • shutdownNow

        public java.util.List<java.lang.Runnable> shutdownNow()
        Stops any new tasks from being submitted to the pool. If any tasks are waiting for execution they will be prevented from being run. If a task is currently running it will be allowed to finish (though this call will not block waiting for it to finish).
        Returns:
        returns a list of runnables which were waiting in the queue to be run at time of shutdown
      • awaitTermination

        public void awaitTermination()
                              throws java.lang.InterruptedException
        Block until the thread pool has shutdown and all threads have been stopped. If neither shutdown() or shutdownNow() is invoked, then this will block forever.
        Throws:
        java.lang.InterruptedException - Thrown if blocking thread is interrupted waiting for shutdown
      • awaitTermination

        public boolean awaitTermination​(long timeoutMillis)
                                 throws java.lang.InterruptedException
        Block until the thread pool has shutdown and all threads have been stopped. If neither shutdown() or shutdownNow() is invoked, then this will block until the timeout is reached.
        Parameters:
        timeoutMillis - time to block and wait for thread pool to shutdown
        Returns:
        true if the pool has shutdown, false if timeout was reached
        Throws:
        java.lang.InterruptedException - Thrown if blocking thread is interrupted waiting for shutdown