com.wutka.util
Class ResourcePool

java.lang.Object
  |
  +--com.wutka.util.ResourcePool

public class ResourcePool
extends java.lang.Object

ResourcePool holds a common set of resources that are given out in first-in-first-out order. The pool assumes that all resources are equally weighted. Since some resource pools may need to allocate additional resources when no more are available, the getOverFlowResource method can be overridden to create new ones.

When the resource is allocated, the requesting thread can associate an object with the allocated resource. One way to make use of this feature is to store the name of the method that is requesting the resource. When debugging code, you may want to look at the allocated resources and what methods have allocated them. Someties you can find methods that aren't releasing resources after allocating them.

If you allocate resources within a try block, you should release them in the finally section of the block. That way, the resource is released no matter what exception is thrown.


Field Summary
protected  java.util.Vector availableResources
          The resources that are currently free
protected  java.util.Vector usedResourceInfo
          User-defined info about a resource that has been allocated
protected  java.util.Vector usedResources
          Resources that have been allocated out of the pool
 
Constructor Summary
ResourcePool()
          Creates a new resource pool
 
Method Summary
 void addResource(java.lang.Object resource)
          Adds another resource to the pool
protected  void doNotify()
          Performs a notifyAll (which requires a synchronized method)
protected  void doWait(long timeout)
          Performs a wait for a specified number of milliseconds.
protected  void doWakeup()
           
 java.util.Vector getAvailableResources()
          Return a copy of the resources in the free list
protected  java.lang.Object getOverflowResource()
          Allocates a resource when the pool is empty.
 java.lang.Object getResource()
          Requests a resource from the pool, waiting forever if one is not available.
 java.lang.Object getResource(long timeout)
          Requests a resource from the pool, waiting forever if one is not available.
 java.lang.Object getResource(java.lang.Object allocationInfo)
          Requests a resource from the pool, waiting forever if one is not available.
 java.lang.Object getResource(java.lang.Object allocationInfo, long timeout)
          Requests a resource from the pool, waiting forever if one is not available.
protected  java.lang.Object getResourceFromList(java.lang.Object allocationInfo)
          Grabs a resource from the free list and moves it to the used list.
 java.util.Vector getUsedResourceInfo()
          Return a copy of the associated info for the used resources
 java.util.Vector getUsedResources()
          Return a copy of the resources in the used list
 void releaseResource(java.lang.Object resource)
          Releases a resource back to the pool of available resources
 void removeResource(java.lang.Object resource)
          Removes a resource from the pool
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

availableResources

protected java.util.Vector availableResources
The resources that are currently free

usedResources

protected java.util.Vector usedResources
Resources that have been allocated out of the pool

usedResourceInfo

protected java.util.Vector usedResourceInfo
User-defined info about a resource that has been allocated
Constructor Detail

ResourcePool

public ResourcePool()
Creates a new resource pool
Method Detail

addResource

public void addResource(java.lang.Object resource)
Adds another resource to the pool
Parameters:
resource - The resource being added

removeResource

public void removeResource(java.lang.Object resource)
Removes a resource from the pool
Parameters:
resource - The resource to be removed

getOverflowResource

protected java.lang.Object getOverflowResource()
Allocates a resource when the pool is empty. By default, this method returns null, indicating that the requesting thread must wait. This allows a thread pool to expand when necessary, allowing for spikes in activity.
Returns:
A new resource, or null to force the requester to wait

getResource

public java.lang.Object getResource()
Requests a resource from the pool, waiting forever if one is not available. No extra information is associated with the allocated resource.
Returns:
The allocated resource

getResource

public java.lang.Object getResource(java.lang.Object allocationInfo)
Requests a resource from the pool, waiting forever if one is not available.
Parameters:
allocationInfo - A value associated with the allocated resource
Returns:
The allocated resource

getResource

public java.lang.Object getResource(long timeout)
Requests a resource from the pool, waiting forever if one is not available.
Parameters:
timeout - The maximum amount of time (in milliseconds) to wait for the resource
Returns:
The allocated resource

getResourceFromList

protected java.lang.Object getResourceFromList(java.lang.Object allocationInfo)
Grabs a resource from the free list and moves it to the used list. This method is really the core of the resource pool. The rest of the class deals with synchronization around this method.
Parameters:
allocationInfo - The info to be associated with the allocated resource
Returns:
The allocated resource

doWait

protected void doWait(long timeout)
Performs a wait for a specified number of milliseconds.
Parameters:
timeout - The number of milliseconds to wait (wait forever if timeout < 0)

getResource

public java.lang.Object getResource(java.lang.Object allocationInfo,
                                    long timeout)
Requests a resource from the pool, waiting forever if one is not available.
Parameters:
allocationInfo - A value associated with the allocated resource
timeout - The maximum amount of time (in milliseconds) to wait for the resource
Returns:
The allocated resource

releaseResource

public void releaseResource(java.lang.Object resource)
Releases a resource back to the pool of available resources
Parameters:
resource - The resource to be returned to the pool

doNotify

protected void doNotify()
Performs a notifyAll (which requires a synchronized method)

doWakeup

protected void doWakeup()

getAvailableResources

public java.util.Vector getAvailableResources()
Return a copy of the resources in the free list
Returns:
The resources in the free list

getUsedResources

public java.util.Vector getUsedResources()
Return a copy of the resources in the used list
Returns:
The resources in the used list

getUsedResourceInfo

public java.util.Vector getUsedResourceInfo()
Return a copy of the associated info for the used resources
Returns:
The info associated with the allocated resources