org.apache.felix.eventadmin.impl.dispatch
Class CacheThreadPool

java.lang.Object
  extended by org.apache.felix.eventadmin.impl.dispatch.CacheThreadPool
All Implemented Interfaces:
ThreadPool

public class CacheThreadPool
extends Object
implements ThreadPool

An implementation of a thread pool that uses a fixed number of cached threads but will spin-off new threads as needed. The underlying assumption is that threads that have been created more recently will be available sooner then older threads hence, once the pool size is reached older threads will be decoupled from the pool and the newly created are added to it.

Author:
Felix Project Team

Constructor Summary
CacheThreadPool(int size)
          The constructor of the pool.
 
Method Summary
 void close()
          Close the pool i.e, stop pooling threads.
 void execute(DispatchTask task, DeliverTask callback)
          Executes the task in a thread out of the pool or a new thread if no pooled thread is available.
 DeliverTask getCallback(Thread thread, DeliverTask defaultCallback)
          Look-up the callback associated with the task that the given thread is currently executing or return the default value that may be null.
 DispatchTask getTask(Thread thread, DispatchTask defaultTask)
          Look-up the task that the given thread is currently executing or return the default value that may be null in case that the thread has not been created by an instance of this class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CacheThreadPool

public CacheThreadPool(int size)
The constructor of the pool. The given size will be used as the max number of pooled threads.

Parameters:
size - The max number of threads pooled at a given time.
Method Detail

execute

public void execute(DispatchTask task,
                    DeliverTask callback)
Executes the task in a thread out of the pool or a new thread if no pooled thread is available. In case that the max size is reached the least recently used (i.e., the longest executing) thread in the pool is decoupled and a new one added to the pool that is used to execute the task.

Specified by:
execute in interface ThreadPool
Parameters:
task - The task to execute
callback - The callback associated with the task
See Also:
ThreadPool.execute(DispatchTask, DeliverTask)

getCallback

public DeliverTask getCallback(Thread thread,
                               DeliverTask defaultCallback)
Look-up the callback associated with the task that the given thread is currently executing or return the default value that may be null.

Specified by:
getCallback in interface ThreadPool
Parameters:
thread - The thread that is currently executing the task for which to return the callback. In case the thread is not created by an instance of this class the default value will be returned.
defaultCallback - The value to return in case that the thread was not created by an instance of this class. May be null
Returns:
The callback associated with the given thread or the default value.
See Also:
ThreadPool.getCallback(Thread, DeliverTask)

getTask

public DispatchTask getTask(Thread thread,
                            DispatchTask defaultTask)
Look-up the task that the given thread is currently executing or return the default value that may be null in case that the thread has not been created by an instance of this class.

Specified by:
getTask in interface ThreadPool
Parameters:
thread - The thread whose currently executed task should be returned.
defaultTask - The default value to be returned in case that the thread was not created by this instance or doesn't currently has a task. May be null
Returns:
The task the given thread is currently executing or the defaultTask
See Also:
ThreadPool.getTask(Thread, DispatchTask)

close

public void close()
Close the pool i.e, stop pooling threads. Note that subsequently, task will still be executed but no pooling is taking place anymore.

Specified by:
close in interface ThreadPool
See Also:
ThreadPool.close()


Copyright © 2006-2011 Apache Software Foundation. All Rights Reserved.