Module xaos.core

Class ThreadPools


  • public class ThreadPools
    extends Object
    Available thread pools to be used by almost any generic applications.
    Author:
    claudio.rosati@esss.se
    • Method Detail

      • cachedThreadPool

        public static ExecutorService cachedThreadPool()
                                                throws IllegalStateException
        Returns a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.

        The returned pool can grow indefinitely and must be used with care. It is best suited for I/O tasks, such as reading and writing databases, web requests, and disk storage. Those tasks often have idle time waiting for the data to be sent or come back.

        Returns:
        The cached thread pool.
        Throws:
        IllegalStateException - If the thread pull should be lazily created and the thread pools were already shutdown by calling shutdown().
        See Also:
        Executors.newCachedThreadPool()
      • fixedThreadPool

        public static ScheduledExecutorService fixedThreadPool()
                                                        throws IllegalStateException
        Returns a thread pool that reuses a fixed number of threads (equals to the number of available processors) operating off a shared unbounded queue, and that can schedule commands to run after a given delay, or to execute periodically. At any point, at most n-threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.

        It is best suited for computation-intensive operations, because it will maintain a conservative number of threads to keep the CPU from being taxed.

        Returns:
        The fixed thread pool.
        Throws:
        IllegalStateException - If the thread pull should be lazily created and the thread pools were already shutdown by calling shutdown().
        See Also:
        Runtime.availableProcessors(), Executors.newScheduledThreadPool(int)
      • singleThreadExecutor

        public static ScheduledExecutorService singleThreadExecutor()
                                                             throws IllegalStateException
        Returns a single-threaded executor (a degenerated pool) that can schedule commands to run after a given delay, or to execute periodically, operating off an unbounded queue (note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks). Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time.
        Returns:
        The single thread pool.
        Throws:
        IllegalStateException - If the thread pull should be lazily created and the thread pools were already shutdown by calling shutdown().
        See Also:
        Executors.newSingleThreadScheduledExecutor()
      • workStealingThreadPool

        public static ExecutorService workStealingThreadPool()
                                                      throws IllegalStateException
        Returns a thread pool that maintains enough threads to support a given parallelism level (equals to the number of available processors), and may use multiple queues to reduce contention. The parallelism level corresponds to the maximum number of threads actively engaged in, or available to engage in, task processing. The actual number of threads may grow and shrink dynamically. A work-stealing pool makes no guarantees about the order in which submitted tasks are executed.

        It is best suited for heavy-load situations, o when a running thread is recursively creating other threads.

        Returns:
        The fixed thread pool.
        Throws:
        IllegalStateException - If the thread pull should be lazily created and the thread pools were already shutdown by calling shutdown().
        See Also:
        Runtime.availableProcessors(), Executors.newWorkStealingPool()