org.dmonix.thread
Class ThreadPool

java.lang.Object
  extended byorg.dmonix.thread.ThreadPool

public class ThreadPool
extends java.lang.Object

This class implements a thread pool.

Copyright: Copyright (c) 2003

Company: dmonix.org

Version:
1.0
Author:
Peter Nerg

Constructor Summary
ThreadPool()
          Creates an empty pool.
ThreadPool(int initialThreadCount)
          Creates a pool with an initial amount of PooledThreads.
 
Method Summary
 void addThread(java.lang.Runnable runnable)
          Adds a new thread to the pool.
 int getCurrentThreadCount()
          Get the current size of the pool.
 int getInitialThreadCount()
          Get the initial size of the pool.
 int getRunningThreadCount()
          Get the amount of running/busy threads.
 PooledThread getThread()
          Get a thread from the pool.
 long getThreadRequests()
          Get the number of times a thread has been requested from the pool.
 void resetThreadRequests()
          Reset the counter for the number of times a thread has been requested from the pool.
 void returnThread(PooledThread t)
          Return a thread to the pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadPool

public ThreadPool()
Creates an empty pool.
Add further PooledThreads using the method addThread

See Also:
addThread(Runnable)

ThreadPool

public ThreadPool(int initialThreadCount)
Creates a pool with an initial amount of PooledThreads.

Parameters:
initialThreadCount - The inital number of threads
Method Detail

addThread

public void addThread(java.lang.Runnable runnable)
Adds a new thread to the pool.
Each Runnable object will be wrapped inside a PooledThread object.
The PooledThread is started immediately after it has been created.

Parameters:
runnable - The runnable to use in the PooledThread
See Also:
org.dmonix.thread.PooledThread.run

getInitialThreadCount

public int getInitialThreadCount()
Get the initial size of the pool.
This value will never change for the thread pool.

Returns:
The initial amount of threads

getCurrentThreadCount

public int getCurrentThreadCount()
Get the current size of the pool.

Returns:
The current amount of threads

getRunningThreadCount

public int getRunningThreadCount()
Get the amount of running/busy threads.

Returns:
The amount of running threads

getThreadRequests

public long getThreadRequests()
Get the number of times a thread has been requested from the pool.

Returns:
The number of thread requests

resetThreadRequests

public void resetThreadRequests()
Reset the counter for the number of times a thread has been requested from the pool.


getThread

public PooledThread getThread()
Get a thread from the pool.
If there are no more free threads the method is put on hold using wait()

Returns:
The thread

returnThread

public void returnThread(PooledThread t)
Return a thread to the pool.
If the pool was previously empty, the method invokes notify() in order to release any thread waiting in the method getThread()

Parameters:
t - The thread to return


Copyright © 1998-2005 dmonix.org Peter Nerg, All Rights Reserved.