org.sape.carbon.services.threadpool
Class DefaultThreadPoolImpl

java.lang.Object
  |
  +--org.sape.carbon.services.threadpool.DefaultThreadPoolImpl
All Implemented Interfaces:
Configurable, Destroyable, FunctionalInterface, Initializable, Startable, Suspendable, ThreadPool

public class DefaultThreadPoolImpl
extends Object
implements ThreadPool, Configurable, Initializable, Startable, Suspendable, Destroyable

This implementation of ThreadPool relys on Doug Lea's PooledExecutor thread pool thread pool implementation. The PooledExecutor is a robust, highly configurable thread pool. Some configurations do not guarantee task execution, however. See Overview of package util.concurrent for more information about the PooledExecutor and other related classes.
Copyright 2002 Sapient

Since:
carbon 2.1
Version:
$Revision: 1.8 $($Author: dvoet $ / $Date: 2003/11/20 18:49:58 $)
Author:
Douglas Voet, Nov 5, 2002

Nested Class Summary
private  class DefaultThreadPoolImpl.TaskRunner
          Implementation of Runnable that wraps the tasks passed into the execute method and is responsible for tracking the task as it is queued and executed.
 
Field Summary
private  ThreadPoolConfiguration config
           
private  List failedTasks
           
private  EDU.oswego.cs.dl.util.concurrent.BoundedBuffer taskQueue
           
private  Component thisComponent
           
private  EDU.oswego.cs.dl.util.concurrent.PooledExecutor threadPool
           
 
Constructor Summary
DefaultThreadPoolImpl()
           
 
Method Summary
 void configure(ComponentConfiguration configuration)
          validates the configuration object and stores it
private  void configureThreadPool()
          configures this.threadPool using values from this.config
 void destroy()
          shutsdown the pool, not waiting to complete any queued tasks
 TaskInfo execute(Runnable task, String taskName)
          Queues a task for execution.
 TaskInfo execute(Runnable task, String taskName, TaskCallback callback)
          Queues a task for execution.
 List getFailedTasks()
          Gets a read-only list of the tasks that failed during execution
 Integer getPoolSize()
          Gets the number of executions threads in the pool
 Integer getQueueSize()
          Gets the number of tasks currently in the queue.
 void initialize(Component thisComponent)
          initializes this.failedTasks and saves thisComponent for later
 void resume()
          Resumes executing queued tasks by starting a thread for each queued task up to the max number of threads for the pool.
 void start()
          Creates the thread pool object and configures it then creates the configured number of threads
 void stop()
          Stops the thread pool and if configured, will wait for queued tasks to execute.
 void suspend()
          interrupts all the threads allowing them to terminate
 void taskFailed(TaskInfo task)
          adds a task to the failed list
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

config

private ThreadPoolConfiguration config

threadPool

private EDU.oswego.cs.dl.util.concurrent.PooledExecutor threadPool

failedTasks

private List failedTasks

thisComponent

private Component thisComponent

taskQueue

private EDU.oswego.cs.dl.util.concurrent.BoundedBuffer taskQueue
Constructor Detail

DefaultThreadPoolImpl

public DefaultThreadPoolImpl()
Method Detail

execute

public TaskInfo execute(Runnable task,
                        String taskName)
Description copied from interface: ThreadPool
Queues a task for execution.

Specified by:
execute in interface ThreadPool
Parameters:
task - the Runnable object that will be executed
taskName - the name of the task, used for logging and tracking
Returns:
an object that can be used to track task status

execute

public TaskInfo execute(Runnable task,
                        String taskName,
                        TaskCallback callback)
Description copied from interface: ThreadPool
Queues a task for execution. Note that callbacks may execute on the pooled thread that executed the task.

Specified by:
execute in interface ThreadPool
Parameters:
task - the Runnable object that will be executed
taskName - the name of the task, used for logging and tracking
callback - the callback object that will be called when the task completes
Returns:
an object that can be used to track task status

getFailedTasks

public List getFailedTasks()
Description copied from interface: ThreadPool
Gets a read-only list of the tasks that failed during execution

Specified by:
getFailedTasks in interface ThreadPool
Returns:
read-only list of the tasks that failed during execution

getQueueSize

public Integer getQueueSize()
Description copied from interface: ThreadPool
Gets the number of tasks currently in the queue. This does not include the number of tasks currently executing.

Specified by:
getQueueSize in interface ThreadPool
Returns:
number of tasks in the queue

getPoolSize

public Integer getPoolSize()
Description copied from interface: ThreadPool
Gets the number of executions threads in the pool

Specified by:
getPoolSize in interface ThreadPool
Returns:
pool size

initialize

public void initialize(Component thisComponent)
                throws Exception
initializes this.failedTasks and saves thisComponent for later

Specified by:
initialize in interface Initializable
Parameters:
thisComponent - the reference to the component that this object is a part of. Store this referece within your Functional Implementation for future use.
Throws:
Exception - when there is a failure to initialize the component

configure

public void configure(ComponentConfiguration configuration)
               throws Exception
validates the configuration object and stores it

Specified by:
configure in interface Configurable
Parameters:
configuration - the configuration for this component
Throws:
Exception - indicates a unknown error

start

public void start()
           throws Exception
Creates the thread pool object and configures it then creates the configured number of threads

Specified by:
start in interface Startable
Throws:
Exception - when there is a failure to start the component

stop

public void stop()
          throws Exception
Stops the thread pool and if configured, will wait for queued tasks to execute.

Specified by:
stop in interface Startable
Throws:
Exception - when there is a failure to stop the exception

resume

public void resume()
            throws Exception
Resumes executing queued tasks by starting a thread for each queued task up to the max number of threads for the pool.

Specified by:
resume in interface Suspendable
Throws:
Exception - when there is a failure to resume the component

suspend

public void suspend()
             throws Exception
interrupts all the threads allowing them to terminate

Specified by:
suspend in interface Suspendable
Throws:
Exception - when there is a failure to suspend the component

destroy

public void destroy()
             throws Exception
shutsdown the pool, not waiting to complete any queued tasks

Specified by:
destroy in interface Destroyable
Throws:
Exception - when there is a failure in destroying the component

taskFailed

public void taskFailed(TaskInfo task)
adds a task to the failed list


configureThreadPool

private void configureThreadPool()
configures this.threadPool using values from this.config



Copyright 1999-2003 Sapient Corporation. All Rights Reserved.