Class GThreadPool


  • public class GThreadPool
    extends java.lang.Object
    Class for managing and sharing thread pools. The GThreadPool is simplified version of the ThreadPoolExecutor, which can be confusing to use with its many configuration parameters. The GThreadPool has a simple behavior that is controlled by only two configuration parameters - the minimum number of threads and the maximum number of threads.

    The simple behavior for when new tasks are submitted:
    1) If there any idle threads, use that thread.
    2) If all existing threads are busy and the number of threads is less than max threads, add a new thread and use it.
    3) if all threads are busy and there are max number of threads, queue the item until a thread becomes free.

    The simple behavior for when tasks are completed by a thread:
    1) If there are tasks in the queue, start processing a new item in the newly freed thread.
    2) if there are more threads that min threads, allow this thread to die if no new jobs arrive before the "KEEP ALIVE" time expires which is currently 15 seconds.
    3) if there are min threads or less, allow this thread to wait forever for a new job to arrive.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.concurrent.Executor getExecutor()
      Returns the Executor used by this thread pool.
      int getMaxThreadCount()
      Returns the maximum number of threads to use in this thread pool.
      int getMinThreadCount()
      Returns the minimum number of threads to keep alive in this thread pool.
      static GThreadPool getPrivateThreadPool​(java.lang.String name)
      Creates a new, private thread pool with the given name.
      static GThreadPool getSharedThreadPool​(java.lang.String name)
      Returns a shared GThreadPool.
      boolean isPrivate()
      Returns true if this is not a shared thread pool.
      static java.util.concurrent.CompletableFuture<java.lang.Void> runAsync​(java.lang.String poolName, java.lang.Runnable r)
      Runs the given runnable in a background thread using a shared thread pool of the given name.
      void setMaxThreadCount​(int maxThreadCount)
      Sets the max number of threads to use in this thread pool.
      void setMinThreadCount​(int minThreadCount)
      Sets the minimum number of threads to keep alive in this thread pool.
      void shutdownNow()  
      java.util.concurrent.Future<?> submit​(java.lang.Runnable task)
      Submits a runnable to be executed by this thread pool.
      <T> java.util.concurrent.Future<T> submit​(java.lang.Runnable task, T result)
      Submits a runnable to be executed by this thread pool.
      <T> java.util.concurrent.Future<T> submit​(java.util.concurrent.Callable<T> task)
      Submits a callable to be executed by this thread pool.
      void submit​(java.util.concurrent.FutureTask<?> futureTask)
      Submits a FutreTask to be executed by a thread in this thread pool.
      • Methods inherited from class java.lang.Object

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

      • getPrivateThreadPool

        public static GThreadPool getPrivateThreadPool​(java.lang.String name)
        Creates a new, private thread pool with the given name.
        Parameters:
        name - the name of the thread pool
        Returns:
        a private GThreadPool with the given name.
      • getSharedThreadPool

        public static GThreadPool getSharedThreadPool​(java.lang.String name)
        Returns a shared GThreadPool. If a shared GThreadPool already exists with the given name, it is returned. Otherwise, a new shared GThreadPool is created and returned.
        Parameters:
        name - the name of the GThreadPool.
        Returns:
        a shared GThreadPool with the given name.
      • runAsync

        public static java.util.concurrent.CompletableFuture<java.lang.Void> runAsync​(java.lang.String poolName,
                                                                                      java.lang.Runnable r)
        Runs the given runnable in a background thread using a shared thread pool of the given name.
        Parameters:
        poolName - the thread pool name
        r - the runnable
        Returns:
        the future
      • setMaxThreadCount

        public void setMaxThreadCount​(int maxThreadCount)
        Sets the max number of threads to use in this thread pool. The default is the number of processors + 1.
        Parameters:
        maxThreadCount - the maximum number of threads to use in this thread pool.
      • getMinThreadCount

        public int getMinThreadCount()
        Returns the minimum number of threads to keep alive in this thread pool.
        Returns:
        the minimum number of threads to keep alive in this thread pool.
      • setMinThreadCount

        public void setMinThreadCount​(int minThreadCount)
        Sets the minimum number of threads to keep alive in this thread pool.
        Parameters:
        minThreadCount - the minimum number of threads to keep alive in this thread pool.
      • getMaxThreadCount

        public int getMaxThreadCount()
        Returns the maximum number of threads to use in this thread pool.
        Returns:
        the maximum number of threads to use in this thread pool.
      • submit

        public void submit​(java.util.concurrent.FutureTask<?> futureTask)
        Submits a FutreTask to be executed by a thread in this thread pool.
        Parameters:
        futureTask - the future task to be executed.
      • submit

        public java.util.concurrent.Future<?> submit​(java.lang.Runnable task)
        Submits a runnable to be executed by this thread pool.
        Parameters:
        task - the runnable to be executed.
        Returns:
        a Future for that runnable.
      • submit

        public <T> java.util.concurrent.Future<T> submit​(java.lang.Runnable task,
                                                         T result)
        Submits a runnable to be executed by this thread pool.
        Parameters:
        task - the runnable to be executed.
        result - the result to be returned after the runnable has executed.
        Returns:
        a Future for that runnable.
      • submit

        public <T> java.util.concurrent.Future<T> submit​(java.util.concurrent.Callable<T> task)
        Submits a callable to be executed by this thread pool.
        Parameters:
        task - the callable to be executed.
        Returns:
        a Future for that callable.
      • shutdownNow

        public void shutdownNow()
      • isPrivate

        public boolean isPrivate()
        Returns true if this is not a shared thread pool.
        Returns:
        true if this is not a shared thread pool.
      • getExecutor

        public java.util.concurrent.Executor getExecutor()
        Returns the Executor used by this thread pool.

        Note: normal usage of this thread pool contraindicates accessing the executor of this pool. For managing your own jobs, you should use the method on this class directly. The intent of this method is to provide access to the executor so that it may be passed to other asynchronous APIs, such as the CompletableFuture.

        Returns:
        the executor