Class ThreadPool

java.lang.Object
jadex.concurrent.ThreadPool
All Implemented Interfaces:
IThreadPool

public class ThreadPool extends Object implements IThreadPool
A thread pool manages pool and saves resources and time by precreating and reusing pool.
  • Field Details

    • PROFILING

      public static final boolean PROFILING
      Enable call profiling.
      See Also:
    • calls

      protected static Map<Class<?>,Integer> calls
      Service calls per runnable class.
    • threadcnt

      protected static int threadcnt
      The thread number.
    • poolcnt

      protected static int poolcnt
      The static thread pool number.
    • group

      protected ThreadGroup group
      The thread group.
    • strategy

      protected jadex.common.IPoolStrategy strategy
      The strategy.
    • pool

      protected List<ThreadPool.ServiceThread> pool
      The pool of service threads.
    • parked

      protected List<ThreadPool.ServiceThread> parked
      The list of threads not used.
    • tasks

      protected jadex.collection.IBlockingQueue<Runnable> tasks
      The tasks to execute.
    • running

      protected volatile boolean running
      The running flag.
    • daemon

      protected boolean daemon
      The daemon flag.
    • enqueuetimes

      protected Map<Runnable,Long> enqueuetimes
      The current throughput of the pool (average of waiting times).
    • maxparked

      protected int maxparked
      The maximum number of parked threads.
    • timer

      protected Timer timer
      Rescue timer that checks if progress is made and tasks are scheduled.
    • maxwait

      protected long maxwait
      The time a task should maximum wait.
    • listeners

      protected List<jadex.common.IChangeListener<Void>> listeners
      The finished listeners.
    • finished

      protected boolean finished
      Boolean if already finished.
  • Constructor Details

    • ThreadPool

      public ThreadPool()
      Create a new thread pool.
    • ThreadPool

      public ThreadPool(jadex.common.IPoolStrategy strategy)
      Create a new thread pool.
    • ThreadPool

      public ThreadPool(boolean daemon, jadex.common.IPoolStrategy strategy)
      Create a new thread pool.
    • ThreadPool

      public ThreadPool(boolean daemon, jadex.common.IPoolStrategy strategy, long maxwait)
      Create a new thread pool.
  • Method Details

    • isRunning

      public boolean isRunning()
      Test if the thread pool is running.
      Specified by:
      isRunning in interface IThreadPool
    • execute

      public void execute(Runnable task)
      Execute a task in its own thread.
      Specified by:
      execute in interface IThreadPool
      Parameters:
      task - The task to execute.
    • executeForever

      public void executeForever(Runnable task)
      Execute a task in its own thread. The pool expects the thread executing the task to never return. Preferably use this method if you want to permanently retrieve a thread e.g. for repeated blocking operations.
      Specified by:
      executeForever in interface IThreadPool
      Parameters:
      task - The task to execute.
    • dispose

      public void dispose()
      Shutdown the task pool
      Specified by:
      dispose in interface IThreadPool
    • toString

      public String toString()
      Get the string representation.
      Overrides:
      toString in class Object
      Returns:
      The string representation.
    • addThreads

      protected void addThreads(int num)
      Create some pool.
      Parameters:
      num - The number of pool.
    • addThread

      protected void addThread()
    • getTask

      protected Runnable getTask(Thread thread)
      The task for a given thread.
    • addFinishListener

      public void addFinishListener(jadex.common.IChangeListener<Void> listener)
      Add a finish listener;
      Specified by:
      addFinishListener in interface IThreadPool
    • notifyFinishListeners

      protected void notifyFinishListeners()
      Notify the finish listeners.
    • main

      public static void main(String[] args)
      Main for testing.
      Parameters:
      args - The arguments.