Class ThreadPool


  • public class ThreadPool
    extends java.lang.Object
    A thread pool that can be assigned work.

    Work is handled in a FIFO manner, and the pool is intended to be very simple and basic.

    For more complex behaviour please look at the ThreadTask type,
    which has a higher abstraction for adding tasks to a ThreadPool.
    • Constructor Summary

      Constructors 
      Constructor Description
      ThreadPool()
      Create new thread pool using the given number of threads.

      If no argument is given, the maximum number of hardware threads on the system is used
      (number of CPUs, cores or hyperthreading units).

      If number of threads is set to 0, the addWork function will be blocking (work is executed
      in thread that invokes addWork).

      ThreadPool​(int threads)
      Create new thread pool using the given number of threads.

      If no argument is given, the maximum number of hardware threads on the system is used
      (number of CPUs, cores or hyperthreading units).

      If number of threads is set to 0, the addWork function will be blocking (work is executed
      in thread that invokes addWork).

      ThreadPool​(long cPtr, boolean cMemoryOwn)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addWork​(SWIGTYPE_p_boost__functionT_void_frw__common__ThreadPool_pF_t work)
      Add work to the thread pool.
      void delete()  
      static long getCPtr​(ThreadPool obj)  
      long getNumberOfThreads()
      Get number of threads in the pool.
      long getQueueSize()
      Get the number of current tasks in the queue (tasks are removed from queue when
      done).
      boolean isStopping()
      Check if work tasks are supposed to shut itself down.

      This function should be called from long-running worker functions to let them shut down
      gracefully.

      void stop()
      Stop processing more work in the queue, and try to stop running work if possible.

      Long-running work should check if the isStopping function returns true and shut down
      gracefully.

      Interrupts are issued, so if there is interruption points in the WorkFunction,
      the work should check for boost::thread_interrupted exceptions and shut down gracefully.
      void waitForEmptyQueue()
      Wait until the task queue becomes empty.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ThreadPool

        public ThreadPool​(long cPtr,
                          boolean cMemoryOwn)
      • ThreadPool

        public ThreadPool​(int threads)
        Create new thread pool using the given number of threads.

        If no argument is given, the maximum number of hardware threads on the system is used
        (number of CPUs, cores or hyperthreading units).

        If number of threads is set to 0, the addWork function will be blocking (work is executed
        in thread that invokes addWork).

        Parameters:
        threads - number of threads to use - default is the number of hardware threads
        available on the system.
      • ThreadPool

        public ThreadPool()
        Create new thread pool using the given number of threads.

        If no argument is given, the maximum number of hardware threads on the system is used
        (number of CPUs, cores or hyperthreading units).

        If number of threads is set to 0, the addWork function will be blocking (work is executed
        in thread that invokes addWork).

    • Method Detail

      • getCPtr

        public static long getCPtr​(ThreadPool obj)
      • delete

        public void delete()
      • getNumberOfThreads

        public long getNumberOfThreads()
        Get number of threads in the pool.
      • stop

        public void stop()
        Stop processing more work in the queue, and try to stop running work if possible.

        Long-running work should check if the isStopping function returns true and shut down
        gracefully.

        Interrupts are issued, so if there is interruption points in the WorkFunction,
        the work should check for boost::thread_interrupted exceptions and shut down gracefully.
      • isStopping

        public boolean isStopping()
        Check if work tasks are supposed to shut itself down.

        This function should be called from long-running worker functions to let them shut down
        gracefully.

        Returns:
        true if thread should shut down.
      • getQueueSize

        public long getQueueSize()
        Get the number of current tasks in the queue (tasks are removed from queue when
        done).
        Returns:
        the number of current tasks.
      • waitForEmptyQueue

        public void waitForEmptyQueue()
        Wait until the task queue becomes empty.