Class Executors

java.lang.Object
com.aoapps.concurrent.Executors
All Implemented Interfaces:
AutoCloseable

public class Executors extends Object implements AutoCloseable
Provides a central set of executors for use by any number of projects. These executors use daemon threads and will not keep the JVM alive. The executors are automatically shutdown using shutdown hooks. The executors are also immediately shutdown when the last instance is closed.

Also allows for delayed execution of tasks using an internal Timer.

  • Constructor Details

    • Executors

      public Executors()
      Create a new instance of the executor service. close() must be called when done with the instance. This should be done in a try-with-resources, try-finally, or strong equivalent, such as Servlet.destroy().

      Internally, threads are shared between executor instances. The threads are only shutdown when the last executor is closed.

      See Also:
  • Method Details

    • newInstance

      @Deprecated public static Executors newInstance()
      Deprecated.
      Just use constructor directly. Subclasses are now allowed, too.
      Creates a new executors.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getPreferredConcurrency

      public int getPreferredConcurrency()
      Gets the preferred concurrency for this executor. Does not change for the life of the executor, but will be updated should the last executor be closed and another created.

      This will always be 1 on a single-CPU system, not a multiple of CPU count.

    • wrap

      protected <T> Callable<T> wrap(Callable<T> task)
      Wraps the task with any other necessary tasks to prepare or cleanup for the task.
      See Also:
    • wrap

      protected Runnable wrap(Runnable task)
      Wraps the task with any other necessary tasks to prepare or cleanup for the task.
      See Also:
    • getUnbounded

      public Executor getUnbounded()
      Gets the unbounded executor. This is most appropriate for I/O bound tasks, especially higher latency I/O like wide area networks.
    • getPerProcessor

      public Executor getPerProcessor()
      An executor service that will execute at most two tasks per processor on multi-CPU systems or one task on single-CPU systems.

      This should be used for CPU-bound tasks that generally operate non-blocking. If a thread blocks or deadlocks, it can starve the system entirely - use this cautiously. For example, do not use it for things like disk I/O or network I/O (including writes - they can block, too, once the network buffers are full).

      When a task is submitted by a thread that is already part of a per-processor executor, it will be invoked on a different per-processor executor to avoid potential deadlock. This means the total number of threads can exceed two tasks per processor, but it will remain bounded as a function of:

      maxThreads = maxPerProcessorDepth * numProcessors * 2

      Where maxPerProcessorDepth is a function of the number of times a per-processor task adds a per-processor task of its own.

      See Also:
    • getSequential

      public Executor getSequential()
      A sequential implementation of executor that performs no concurrent processing. All tasks are performed on the thread calling Future.get().

      Important: A task submitted to this sequential executor is only processed when Future.get() is called. Thus, a task submitted without a call to Future.get() is never executed. This is in stark contrast to other executors, which will complete the task regardless.

      Note: Timeout not implemented

    • close

      public void close()
      Closes this executor service instance. Once closed, no additional tasks may be submitted. Any overriding method must call super.close().

      If this is the last active executor, the underlying threads will also be shutdown. This shutdown may wait up to (1 + numPerProcessorPools) * CLOSE_WAIT_NANOS for clean termination of all threads.

      If already closed, no action will be taken and no exception thrown.

      Specified by:
      close in interface AutoCloseable
      See Also:
      • CLOSE_WAIT_NANOS