Class ThreadPool


  • @SupportedAll
    public class ThreadPool
    extends Object

    This thread pool maintains a number of threads that run the tasks from a task queue one by one. The tasks are handled in asynchronous mode, which means it will not block the main thread to proceed while the task is being processed by the thread pool.

    This thread pool has a fixed size of threads. It maintains all the tasks to be executed in a task queue. Each thread then in turn gets a task from the queue to execute. If the tasks in the task queue reaches a certain number(the threshold value), it will log an error message and ignore the new incoming tasks until the number of un-executed tasks is less than the threshold value. This guarantees the thread pool will not use up the system resources under heavy load.

    • Constructor Detail

      • ThreadPool

        public ThreadPool​(String name,
                          int poolSize,
                          int threshold,
                          boolean daemon,
                          org.slf4j.Logger debug)
        Constructs a thread pool with given parameters.
        Parameters:
        name - name of the thread pool.
        poolSize - the thread pool size, indicates how many threads are created in the pool.
        threshold - the maximum size of the task queue in the thread pool.
        daemon - set the threads as daemon if true; otherwise if not.
        debug - Debug object to send debugging message to.
    • Method Detail

      • createThreads

        protected void createThreads​(int threadsToCreate)
        Create thread for the pool.
        Parameters:
        threadsToCreate - number of threads of the pool after creation
      • run

        public final void run​(Runnable task)
                       throws com.iplanet.am.util.ThreadPoolException
        Runs a user defined task.
        Parameters:
        task - user defined task.
        Throws:
        com.iplanet.am.util.ThreadPoolException - If there was an error while submitting the task to the threadpool.
      • deductCurrentThreadCount

        protected void deductCurrentThreadCount()
      • returnThread

        protected void returnThread​(com.iplanet.am.util.ThreadPool.WorkerThread t)
      • shutdown

        public void shutdown()
      • getCurrentThreadCount

        public int getCurrentThreadCount()
      • getCurrentSize

        public int getCurrentSize()