org.curjent.agent
Class ReusableTask

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

public class ReusableTask
extends Object
implements AgentTasks

Source for a task that can be used concurrently by multiple threads. Since one task is used by multiple threads, the task is responsible for synchronizing access to any mutable state.

This implementation is not thread-safe.


Field Summary
private  int count
          Keeps track of how many times the task has been acquired.
private  int max
          Maximum number of times the task can be acquired.
private  Object task
          Single task used by multiple threads of execution.
 
Constructor Summary
ReusableTask(Object task, int max)
          Initializes this instance with the one reusable task and the maximum number of times it can be acquired.
 
Method Summary
 Object acquire()
          Increments the number of outstanding acquires and returns the reusable task.
 void release(Object task)
          Decrements the number of outstanding acquires.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

task

private final Object task
Single task used by multiple threads of execution.


max

private final int max
Maximum number of times the task can be acquired.


count

private int count
Keeps track of how many times the task has been acquired. This value is always between 0 and max, inclusive.

Constructor Detail

ReusableTask

public ReusableTask(Object task,
                    int max)
Initializes this instance with the one reusable task and the maximum number of times it can be acquired. If max is greater than 1, the given task must be thread-safe.

Throws:
NullPointerException - task is null.
IllegalArgumentException - max is 0 or negative.
Method Detail

acquire

public Object acquire()
               throws Throwable
Increments the number of outstanding acquires and returns the reusable task. Returns null if max has been reached.

Specified by:
acquire in interface AgentTasks
Throws:
Throwable - An unexpected error.
See Also:
Agent.newInstance(AgentLoader, Class[], AgentTasks, Class)

release

public void release(Object task)
             throws Throwable
Decrements the number of outstanding acquires.

Specified by:
release in interface AgentTasks
Throws:
IllegalStateException - The given task is not the same as the task this instance was constructed with.
IllegalStateException - The number of releases has exceeded the number of outstanding acquires.
Throwable - An unexpected error.


Copyright 2009-2011 Tom Landon
Apache License 2.0