Holger's Java API

## com.antelmann.util
Class ThreadPool

    java.lang.Object
      com.antelmann.util.ThreadPool

All Implemented Interfaces:
  Executor

    public class ThreadPool

extends Object
implements Executor

uses a pool of threads to execute tasks asynchronously and concurrently. ThreadPool was implemented before JDK1.5 and is somewhat simillar to `ThreadPoolExecutor`

Author:
  Holger Antelmann
See Also:
  `ThreadWorker`

Constructor Summary
`ThreadPool(int numberOfThreads)`
`ThreadPool(int numberOfThreads, boolean asDaemon)`

Method Summary
`void` `addListener(ThreadWorker.Listener listener)`
`int` `addThread()` adds one ThreadWorker to the pool and returns the current number of threads
`void` `execute(Runnable task)` calls `runTask(task)`; this method enables usability with jdk1.5
`void` `finishAll(boolean now)` removes all ThreadWorkers, which will all end either after the current or the last queued task - depending on the 'now' parameter.
`ThreadWorker.Listener[]` `getListeners()`
`int` `getNumberOfThreads()` returns the number of all threads currently in this pool
`int` `getQueueSize()` total queue size over all threads
`int` `idleThreads()` returns the number of threads that are currently idle
`boolean` `isActive()` returns true if at least one thread in the pool is currently active
`boolean` `removeIdleThread()` attempts to remove a single ThreadWorker from the pool.
`int` `removeIdleThreads()` removes all threads that are currently idle.
`void` `removeListener(ThreadWorker.Listener listener)`
`void` `runTask(Runnable task)`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### ThreadPool

    public ThreadPool(int numberOfThreads)

### ThreadPool

    public ThreadPool(int numberOfThreads,
                      boolean asDaemon)

Method Detail

### isActive

    public boolean isActive()

  returns true if at least one thread in the pool is currently active

### getNumberOfThreads

    public int getNumberOfThreads()

  returns the number of all threads currently in this pool

### addThread

    public int addThread()

  adds one ThreadWorker to the pool and returns the current number of threads

### idleThreads

    public int idleThreads()

  returns the number of threads that are currently idle

### removeIdleThreads

    public int removeIdleThreads()

  removes all threads that are currently idle. A thread can only be removed if idle.

  Returns:
    the number of threads removed

### removeIdleThread

    public boolean removeIdleThread()

  attempts to remove a single ThreadWorker from the pool. If all threads but one have already been removed from the pool, nothing happens and false is returned. This method first attempts to remove an idle thread - if found. If all threads are active, the one with the smallest queue size is removed.

  Returns:
    true only if a thread was indeed removed from the pool

### finishAll

    public void finishAll(boolean now)

  removes all ThreadWorkers, which will all end either after the current or the last queued task - depending on the 'now' parameter.

### getQueueSize

    public int getQueueSize()

  total queue size over all threads

### addListener

    public void addListener(ThreadWorker.Listener listener)

### getListeners

    public ThreadWorker.Listener[] getListeners()

### removeListener

    public void removeListener(ThreadWorker.Listener listener)

### execute

    public void execute(Runnable task)

  calls `runTask(task)`; this method enables usability with jdk1.5

  Specified by:
    `execute` in interface `Executor`

### runTask

    public void runTask(Runnable task)
                 throws IllegalStateException

  Throws:
    `IllegalStateException`

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads