org.curjent.agent
Class DynamicTasks

java.lang.Object
  extended by org.curjent.agent.DynamicTasks
All Implemented Interfaces:
AgentTasks

public abstract class DynamicTasks
extends Object
implements AgentTasks

Source for dynamically created tasks. This abstract class defines two method templates for subclasses: create and discard. This class is useful for creating and caching tasks that are too expensive in time or memory to create up front.

This implementation is not thread-safe.


Field Summary
private  int acquireCount
          Number of acquired tasks.
private  int acquireMax
          Maximum number of tasks that can be acquired.
private  int cacheMax
          Maximum number of tasks that can be cached.
private  LinkedList<Object> taskCache
          Cache of tasks.
 
Constructor Summary
protected DynamicTasks(int acquireMax, int cacheMax, Collection<?> cacheSeed)
          Initializes the cache of tasks.
 
Method Summary
 Object acquire()
          Acquires a task.
protected abstract  Object create()
          Creates a new task.
protected  void discard(Object task)
          Discards a task.
 void release(Object task)
          Releases a task.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

acquireMax

private final int acquireMax
Maximum number of tasks that can be acquired.


cacheMax

private final int cacheMax
Maximum number of tasks that can be cached.


taskCache

private final LinkedList<Object> taskCache
Cache of tasks. Size never exceeds cacheMax.


acquireCount

private int acquireCount
Number of acquired tasks.

Constructor Detail

DynamicTasks

protected DynamicTasks(int acquireMax,
                       int cacheMax,
                       Collection<?> cacheSeed)
Initializes the cache of tasks. The cache is initialized with a copy of the given (possibly empty or null) cacheSeed. When a task is acquired, it is taken from the cache if available. If the cache is empty, a new task is created. As many as acquireMax tasks may be acquired. When a task is released, it is added to the cache. The cache can hold as many as cacheMax tasks. If the cache is full, the released task is discarded.

Parameters:
acquireMax - Maximum number of tasks that can be acquired.
cacheMax - Maximum number of tasks that can be cached.
cacheSeed - Initial cache of tasks. Possibly empty or null.
Throws:
IllegalArgumentException - acquireMax <= 0
IllegalArgumentException - acquireMax < cacheMax
IllegalArgumentException - cacheMax < 0
IllegalArgumentException - cacheSeed.size() > acquireMax
Method Detail

create

protected abstract Object create()
                          throws Throwable
Creates a new task. Called when the cache is empty. If this method returns null, the acquire method returns null, too. In this way the subclass can dynamically control how many tasks are created and acquired. This may be fewer than or equal to acquireMax but never more.

Throws:
Throwable
See Also:
acquire()

discard

protected void discard(Object task)
                throws Throwable
Discards a task. Called when the cache is full. The default implementation does nothing.

Throws:
Throwable
See Also:
release(Object)

acquire

public final Object acquire()
                     throws Throwable
Acquires a task. Returns a task from the cache if available. If the cache is empty, attempts to create and return a new task. If create() returns null, this method returns null, too.

Specified by:
acquire in interface AgentTasks
Throws:
Throwable - An unexpected error.
See Also:
create()

release

public final void release(Object task)
                   throws Throwable
Releases a task. Returns the task to the cache, or discards the task if the cache is full.

Specified by:
release in interface AgentTasks
Throws:
NullPointerException - task == null
IllegalStateException - acquireCount <= 0
Throwable - An unexpected error.
See Also:
discard(Object)


Copyright 2009-2011 Tom Landon
Apache License 2.0