org.sadun.util.tp
Class ThreadPoolThread

java.lang.Object
  extended byjava.lang.Thread
      extended byorg.sadun.util.tp.ThreadPoolThread
All Implemented Interfaces:
java.lang.Runnable, Terminable

public class ThreadPoolThread
extends java.lang.Thread
implements Terminable

A thread which acts as a pool for other threads. Its run methods simply waits if there aren't runnable start requests.

Albeit it is not type-compatible with ThreadPool (since it extends Thread) this class exposes the same methods.

Version:
1.0
Author:
Cris Sadun

Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ThreadPoolThread(int size)
          Create a pool of daemon threads with the given size and a FIFO waiting queue
ThreadPoolThread(int size, boolean daemon)
          Create a pool with the given size with a FIFO queue
ThreadPoolThread(int size, boolean daemon, Queue queue)
          Create a pool with the given size using the given queue object.
 
Method Summary
 int getQueueSize()
          Return the number of thread currently queued
 boolean isShuttingDown()
          Return true if a shutdown has been requested.
 void run()
           
 void shutdown()
          Unconditionally requests a shutdown.
 int size()
          Return the size of the pool
 boolean start(java.lang.Runnable runnable)
          Adds a runnable object to the pool.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ThreadPoolThread

public ThreadPoolThread(int size,
                        boolean daemon,
                        Queue queue)
Create a pool with the given size using the given queue object.

Parameters:
size - the size of the pool
daemon - if true the pools will be daemon
queue - the queue to use for determining the next thread to instantiate when there are pending start requests.

ThreadPoolThread

public ThreadPoolThread(int size,
                        boolean daemon)
Create a pool with the given size with a FIFO queue

Parameters:
size - the size of the pool
daemon - if true the pools will be daemon

ThreadPoolThread

public ThreadPoolThread(int size)
Create a pool of daemon threads with the given size and a FIFO waiting queue

Parameters:
size - the size of the pool
Method Detail

size

public int size()
Return the size of the pool

Returns:
the size of the pool

getQueueSize

public int getQueueSize()
Return the number of thread currently queued

Returns:
the number of thread currently queued

start

public boolean start(java.lang.Runnable runnable)
Adds a runnable object to the pool. If there's a thread available, the runnable is associated to the thread and started. Else, it is queued, and will run as soon as one thread becomes available.

Parameters:
runnable - the Runnable object to execute
Returns:
true if the runnable is started, false if it's queued.

shutdown

public void shutdown()
Description copied from interface: Terminable
Unconditionally requests a shutdown. The runnable will complete its last atomic operation, and gracefully exit the run() method.

Specified by:
shutdown in interface Terminable

isShuttingDown

public boolean isShuttingDown()
Description copied from interface: Terminable
Return true if a shutdown has been requested.

Specified by:
isShuttingDown in interface Terminable
Returns:
true if a shutdown has been requested

run

public void run()
Specified by:
run in interface java.lang.Runnable