Class Executors


  • public final class Executors
    extends java.lang.Object
    Utility methods to create various Executors.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Executor from​(java.util.concurrent.Executor jdkExecutor)
      Creates a new Executor from the provided jdkExecutor.
      static Executor from​(java.util.concurrent.Executor jdkExecutor, java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
      Creates a new Executor using executor to execute immediate tasks and scheduler to schedule delayed tasks.
      static Executor from​(java.util.concurrent.Executor jdkExecutor, java.util.concurrent.ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel)
      Creates a new Executor using executor to execute immediate tasks and scheduler to schedule delayed tasks.
      static Executor from​(java.util.concurrent.ExecutorService executorService)
      Creates a new Executor from the provided ExecutorService.
      static Executor from​(java.util.concurrent.ExecutorService executorService, boolean mayInterruptOnCancel)
      Creates a new Executor from the provided ExecutorService.
      static Executor from​(java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
      Creates a new Executor from the provided ScheduledExecutorService.
      static Executor from​(java.util.concurrent.ScheduledExecutorService scheduledExecutorService, boolean mayInterruptOnCancel)
      Creates a new Executor from the provided ScheduledExecutorService.
      static Executor immediate()
      Returns an Executor that executes all tasks submitted via Executor.execute(Runnable) immediately by calling Runnable.run() on the calling thread.
      static Executor newCachedThreadExecutor()
      Creates a new Executor that creates as many threads as required but reuses threads when possible.
      static Executor newCachedThreadExecutor​(java.util.concurrent.ThreadFactory threadFactory)
      Creates a new Executor that creates as many threads as required but reuses threads when possible.
      static Executor newFixedSizeExecutor​(int size)
      Creates a new Executor that has a fixed number of threads as specified by the size.
      static Executor newFixedSizeExecutor​(int size, java.util.concurrent.ThreadFactory threadFactory)
      Creates a new Executor that has a fixed number of threads as specified by the size.
      • Methods inherited from class java.lang.Object

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

      • newFixedSizeExecutor

        public static Executor newFixedSizeExecutor​(int size)
        Creates a new Executor that has a fixed number of threads as specified by the size.
        Parameters:
        size - Number of threads used by the newly created Executor.
        Returns:
        A new Executor that will use the size number of threads.
      • newFixedSizeExecutor

        public static Executor newFixedSizeExecutor​(int size,
                                                    java.util.concurrent.ThreadFactory threadFactory)
        Creates a new Executor that has a fixed number of threads as specified by the size.
        Parameters:
        size - Number of threads used by the newly created Executor.
        threadFactory - ThreadFactory to use.
        Returns:
        A new Executor that will use the size number of threads.
      • newCachedThreadExecutor

        public static Executor newCachedThreadExecutor()
        Creates a new Executor that creates as many threads as required but reuses threads when possible.
        Returns:
        A new Executor.
      • newCachedThreadExecutor

        public static Executor newCachedThreadExecutor​(java.util.concurrent.ThreadFactory threadFactory)
        Creates a new Executor that creates as many threads as required but reuses threads when possible.
        Parameters:
        threadFactory - ThreadFactory to use.
        Returns:
        A new Executor.
      • from

        public static Executor from​(java.util.concurrent.Executor jdkExecutor)
        Creates a new Executor from the provided jdkExecutor.

        Delayed task execution will be delegated to a global scheduler, unless passed Executor is an instance of ScheduledExecutorService.

        Task execution will not honor cancellations unless passed Executor is an instance of ExecutorService.

        Long running tasks

        Executor implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the same Executor instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in the Executor.
        Parameters:
        jdkExecutor - Executor to use for executing tasks. The lifetime of this object is transferred to the return value. In other words AsyncCloseable.closeAsync() will call ExecutorService.shutdown() (if possible).
        Returns:
        Executor that wraps the passed jdkExecutor.
      • from

        public static Executor from​(java.util.concurrent.ExecutorService executorService)
        Creates a new Executor from the provided ExecutorService.

        Delayed task execution will be delegated to a global scheduler, unless passed ExecutorService is an instance of ScheduledExecutorService.

        When a running task is cancelled, the thread running it will be interrupted. For overriding this behavior use from(ExecutorService, boolean).

        Long running tasks

        Executor implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the same Executor instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in the Executor.
        Parameters:
        executorService - ExecutorService to use for executing tasks. The lifetime of this object is transferred to the return value. In other words AsyncCloseable.closeAsync() will call ExecutorService.shutdown().
        Returns:
        Executor that wraps the passed executorService.
      • from

        public static Executor from​(java.util.concurrent.ExecutorService executorService,
                                    boolean mayInterruptOnCancel)
        Creates a new Executor from the provided ExecutorService. Delayed task execution will be delegated to a global scheduler, unless passed ExecutorService is an instance of ScheduledExecutorService.

        Long running tasks

        Executor implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the same Executor instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in the Executor.
        Parameters:
        executorService - ExecutorService to use for executing tasks. The lifetime of this object is transferred to the return value. In other words AsyncCloseable.closeAsync() will call ExecutorService.shutdown().
        mayInterruptOnCancel - If set to true, when a task is cancelled, thread running the task will be interrupted.
        Returns:
        Executor that wraps the passed executorService.
      • from

        public static Executor from​(java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
        Creates a new Executor from the provided ScheduledExecutorService. When a running task is cancelled, the thread running it will be interrupted. For overriding this behavior use from(ScheduledExecutorService, boolean).

        Long running tasks

        Executor implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the same Executor instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in the Executor.
        Parameters:
        scheduledExecutorService - ScheduledExecutorService to use for executing tasks. The lifetime of this object is transferred to the return value. In other words AsyncCloseable.closeAsync() will call ExecutorService.shutdown().
        Returns:
        Executor that wraps the passed scheduledExecutorService.
      • from

        public static Executor from​(java.util.concurrent.ScheduledExecutorService scheduledExecutorService,
                                    boolean mayInterruptOnCancel)
        Creates a new Executor from the provided ScheduledExecutorService.

        Long running tasks

        Executor implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the same Executor instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in the Executor.
        Parameters:
        scheduledExecutorService - ScheduledExecutorService to use for executing tasks. The lifetime of this object is transferred to the return value. In other words AsyncCloseable.closeAsync() will call ExecutorService.shutdown().
        mayInterruptOnCancel - If set to true, when a task is cancelled, thread running the task will be interrupted.
        Returns:
        Executor that wraps the passed scheduledExecutorService.
      • from

        public static Executor from​(java.util.concurrent.Executor jdkExecutor,
                                    java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
        Creates a new Executor using executor to execute immediate tasks and scheduler to schedule delayed tasks. When a running task is cancelled, the thread running it will be interrupted. For overriding this behavior use from(java.util.concurrent.Executor, ScheduledExecutorService, boolean). Task execution will not honor cancellations unless passed Executor is an instance of ExecutorService.

        Long running tasks

        Executor implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the same Executor instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in the Executor.
        Parameters:
        jdkExecutor - Executor to use for executing tasks. The lifetime of this object is transferred to the return value. In other words AsyncCloseable.closeAsync() will call ExecutorService.shutdown() (if possible).
        scheduledExecutorService - ScheduledExecutorService to use for executing tasks. The lifetime of this object is transferred to the return value. In other words AsyncCloseable.closeAsync() will call ExecutorService.shutdown().
        Returns:
        A new Executor.
      • from

        public static Executor from​(java.util.concurrent.Executor jdkExecutor,
                                    java.util.concurrent.ScheduledExecutorService scheduledExecutorService,
                                    boolean mayInterruptOnCancel)
        Creates a new Executor using executor to execute immediate tasks and scheduler to schedule delayed tasks. Task execution will not honor cancellations unless passed Executor is an instance of ExecutorService.

        Long running tasks

        Executor implementations are expected to run long running (blocking) tasks which may depend on other tasks submitted to the same Executor instance. In order to avoid deadlocks, it is generally a good idea to not allow task queuing in the Executor.
        Parameters:
        jdkExecutor - Executor to use for executing tasks. The lifetime of this object is transferred to the return value. In other words AsyncCloseable.closeAsync() will call ExecutorService.shutdown() (if possible).
        scheduledExecutorService - ScheduledExecutorService to use for executing tasks. The lifetime of this object is transferred to the return value. In other words AsyncCloseable.closeAsync() will call ExecutorService.shutdown().
        mayInterruptOnCancel - If set to true, when a task is cancelled, thread running the task will be interrupted.
        Returns:
        A new Executor.