kiwi.util
Class ResourcePool

java.lang.Object
  extended bykiwi.util.ResourcePool
Direct Known Subclasses:
TimerPool

public abstract class ResourcePool
extends java.lang.Object

An abstract class that represents a pool of instances of some resource. See TimerPool for an example concrete implementation. Accesses to the pool are threadsafe so there is no possibility of contention for the resource.

Author:
Mark Lindner, PING Software Group
See Also:
Resource, TimerPool

Field Summary
private  java.util.Stack availableList
           
private  java.util.Stack reservedList
           
private  int size
           
 
Constructor Summary
ResourcePool(int size)
          Construct a new ResourcePool of the given size.
 
Method Summary
protected abstract  Resource constructResource()
          Construct an instance of the resource that is managed by this pool.
 int getAvailableResourceCount()
          Get the number of resource instances that are currently available.
 int getTotalResourceCount()
          Get the total number of resource instances in this pool.
 int getUsedResourceCount()
          Get the number of resource instances that are currently in use.
 void releaseResource(Resource resource)
          Release the given resource.
 Resource reserveResource()
          Reserve one instance of the resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

size

private int size

reservedList

private java.util.Stack reservedList

availableList

private java.util.Stack availableList
Constructor Detail

ResourcePool

public ResourcePool(int size)
Construct a new ResourcePool of the given size.

Parameters:
size - The number of instances of a resource to preallocate in this pool.
Method Detail

reserveResource

public Resource reserveResource()
Reserve one instance of the resource. If all instances are currently in use, this method blocks until one becomes available.

Returns:
An instance of the Resource.

releaseResource

public void releaseResource(Resource resource)
Release the given resource. If the resource is not currently reserved, this method does nothing. Note that it is the caller's responsibility to pass the correct resource to this method; the method does not check if the calling thread actually has the specified resource reserved.

Parameters:
resource - The Resource to release.

constructResource

protected abstract Resource constructResource()
Construct an instance of the resource that is managed by this pool. The constructor calls this method repeatedly to pre-build the number of instances specified as its argument.

Returns:
The newly-constructed Resource instance.

getTotalResourceCount

public int getTotalResourceCount()
Get the total number of resource instances in this pool.

Returns:
The total number of instances.

getUsedResourceCount

public int getUsedResourceCount()
Get the number of resource instances that are currently in use.

Returns:
The number of instances that are in use.

getAvailableResourceCount

public int getAvailableResourceCount()
Get the number of resource instances that are currently available.

Returns:
The number of instances that are available.