activemq::concurrent::ThreadPool Class Reference

#include <ThreadPool.h>

Inheritance diagram for activemq::concurrent::ThreadPool:

activemq::concurrent::PooledThreadListener List of all members.

Public Types

typedef std::pair< Runnable *,
TaskListener * > 
Task

Public Member Functions

 ThreadPool (void)
virtual ~ThreadPool (void)
virtual void queueTask (Task task) throw ( exceptions::ActiveMQException )
virtual Task deQueueTask (void) throw ( exceptions::ActiveMQException )
virtual unsigned long getPoolSize (void) const
virtual unsigned long getBacklog (void) const
virtual void reserve (unsigned long size)
virtual unsigned long getMaxThreads (void) const
virtual void setMaxThreads (unsigned long maxThreads)
virtual unsigned long getBlockSize (void) const
virtual void setBlockSize (unsigned long blockSize)
virtual unsigned long getFreeThreadCount (void) const
virtual void onTaskStarted (PooledThread *thread)
virtual void onTaskCompleted (PooledThread *thread)
virtual void onTaskException (PooledThread *thread, exceptions::ActiveMQException &ex)

Static Public Member Functions

static ThreadPoolgetInstance (void)

Static Public Attributes

static const size_t DEFAULT_MAX_POOL_SIZE = 10
static const size_t DEFAULT_MAX_BLOCK_SIZE = 3

Private Member Functions

 LOGCMS_DECLARE (logger)
 LOGCMS_DECLARE (marker)
void AllocateThreads (unsigned long count)

Private Attributes

std::vector< PooledThread * > pool
util::Queue< Taskqueue
unsigned long maxThreads
unsigned long blockSize
bool shutdown
unsigned long freeThreads
Mutex poolLock

Static Private Attributes

static ThreadPool instance

Detailed Description

Defines a Thread Pool object that implements the functionality of pooling threads to perform user tasks. The Thread Poll has max size that it will grow to. The thread pool allocates threads in blocks. When there are no waiting worker threads and a task is queued then a new batch is allocated. The user can specify the size of the blocks, otherwise a default value is used.

When the user queues a task they must also queue a listner to be notified when the task has completed, this provides the user with a mechanism to know when a task object can be freed.

To have the Thread Pool perform a task, the user enqueue's an object that implements the Runnable insterface and one of the worker threads will executing it in its thread context.


Member Typedef Documentation

typedef std::pair<Runnable*, TaskListener*> activemq::concurrent::ThreadPool::Task
 


Constructor & Destructor Documentation

ThreadPool::ThreadPool void   ) 
 

ThreadPool::~ThreadPool void   )  [virtual]
 


Member Function Documentation

void ThreadPool::AllocateThreads unsigned long  count  )  [private]
 

Allocates the requested ammount of Threads, won't exceed maxThreads.

Parameters:
count the number of threads to create

ThreadPool::Task ThreadPool::deQueueTask void   )  throw ( exceptions::ActiveMQException ) [virtual]
 

DeQueue a task to be completed by one of the Pooled Threads. A caller of this method will block until there is something in the tasks queue, therefore care must be taken when calling this function. Normally clients of ThreadPool don't use this, only the PooledThread objects owned by this ThreadPool.

Returns:
object that derives from Runnable
Exceptions:
ActiveMQException 

virtual unsigned long activemq::concurrent::ThreadPool::getBacklog void   )  const [inline, virtual]
 

Returns the current backlog of items in the tasks queue, this is how much work is still waiting to get done.

Returns:
number of outstanding tasks.

virtual unsigned long activemq::concurrent::ThreadPool::getBlockSize void   )  const [inline, virtual]
 

Gets the Max number of threads that can be allocated at a time when new threads are needed.

Returns:
max Thread Block Size

virtual unsigned long activemq::concurrent::ThreadPool::getFreeThreadCount void   )  const [inline, virtual]
 

Returns the current number of available threads in the pool, threads that are performing a user task are considered unavailable. This value could change immeadiately after calling as Threads could finish right after and be available again. This is informational only.

Returns:
totoal free threads

static ThreadPool* activemq::concurrent::ThreadPool::getInstance void   )  [inline, static]
 

Return the one and only Thread Pool instance.

Returns:
The Thread Pool Pointer

virtual unsigned long activemq::concurrent::ThreadPool::getMaxThreads void   )  const [inline, virtual]
 

Get the Max Number of Threads this Pool can contain

Returns:
max size

virtual unsigned long activemq::concurrent::ThreadPool::getPoolSize void   )  const [inline, virtual]
 

Returns the current number of Threads in the Pool, this is how many there are now, not how many are active or the max number that might exist.

Returns:
integer number of threads in existance.

activemq::concurrent::ThreadPool::LOGCMS_DECLARE marker   )  [private]
 

activemq::concurrent::ThreadPool::LOGCMS_DECLARE logger   )  [private]
 

void ThreadPool::onTaskCompleted PooledThread thread  )  [virtual]
 

Called by a pooled thread when it has completed a task and is going back to waiting for another task to run, this will increment the free threads counter.

Parameters:
thread Pointer the the Pooled Thread that is making this call.

Implements activemq::concurrent::PooledThreadListener.

void ThreadPool::onTaskException PooledThread thread,
exceptions::ActiveMQException ex
[virtual]
 

Called by a pooled thread when it has encountered an exception while running a user task, after receiving this notification the callee should assume that the PooledThread is now no longer running.

Parameters:
thread Pointer to the Pooled Thread that is making this call
ex The Exception that occured.

Implements activemq::concurrent::PooledThreadListener.

void ThreadPool::onTaskStarted PooledThread thread  )  [virtual]
 

Called by a pooled thread when it is about to begin executing a new task. This will decrement the available threads counter so that this object knows when there are no more free threads and must create new ones.

Parameters:
thread Pointer to the Pooled Thread that is making this call

Implements activemq::concurrent::PooledThreadListener.

void ThreadPool::queueTask Task  task  )  throw ( exceptions::ActiveMQException ) [virtual]
 

Queue a task to be completed by one of the Pooled Threads. tasks are serviced as soon as a PooledThread is available to run it.

Parameters:
task object that derives from Runnable
Exceptions:
ActiveMQException 

void ThreadPool::reserve unsigned long  size  )  [virtual]
 

Ensures that there is at least the specified number of Threads allocated to the pool. If the size is greater than the MAX number of threads in the pool, then only MAX threads are reservved. If the size is smaller than the number of threads currently in the pool, than nothing is done.

Parameters:
size the number of threads to reserve.

void ThreadPool::setBlockSize unsigned long  blockSize  )  [virtual]
 

Sets the Max number of Threads that can be allocated at a time when the Thread Pool determines that more Threads are needed.

Parameters:
blockSize Max Thread Block Size

void ThreadPool::setMaxThreads unsigned long  maxThreads  )  [virtual]
 

Sets the Max number of threads this pool can contian. if this value is smaller than the current size of the pool nothing is done.

Parameters:
maxThreads total number of threads that can be pooled


Member Data Documentation

unsigned long activemq::concurrent::ThreadPool::blockSize [private]
 

const size_t activemq::concurrent::ThreadPool::DEFAULT_MAX_BLOCK_SIZE = 3 [static]
 

const size_t activemq::concurrent::ThreadPool::DEFAULT_MAX_POOL_SIZE = 10 [static]
 

unsigned long activemq::concurrent::ThreadPool::freeThreads [private]
 

ThreadPool ThreadPool::instance [static, private]
 

unsigned long activemq::concurrent::ThreadPool::maxThreads [private]
 

std::vector< PooledThread* > activemq::concurrent::ThreadPool::pool [private]
 

Mutex activemq::concurrent::ThreadPool::poolLock [private]
 

util::Queue<Task> activemq::concurrent::ThreadPool::queue [private]
 

bool activemq::concurrent::ThreadPool::shutdown [private]
 


The documentation for this class was generated from the following files:
Generated on Thu Aug 3 18:03:31 2006 for activemq-cpp by  doxygen 1.4.5