Package org.jini.rio.resources.resource
Class ResourcePool
java.lang.Object
org.jini.rio.resources.resource.ResourcePool
- Direct Known Subclasses:
ThreadPool
A generic abstract class to handle a finite set of resources
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected LinkedListThe alloced listprotected LinkedListThe free listprotected StringUnique identifier for a resourcePool instanceprotected intThe maximum # of resources allowedprotected intThe minimum # of resources to create at init timeprotected intThe total number of resources currently created -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidallocResource(Object resource) allocates a Resource and performs any initilization required to manage the alloced resourceprotected abstract Objectcreate()Abstract method for creating a specific resource in the sub-classprotected Objectcreates a new Resource and performs any initilization required to manage the resourceprotected voidPre-creates the minimum number of resourcesprotected voidcreateResources(int number) Pre-creates the specified number of resourcesprotected abstract voidAbstract method for disposing a specific resource in the sub-classprotected voiddisposeResource(Object resource) disposes of a Resource and performs any clean-up required to manage the resourceprotected voidfreeResource(Object resource) frees a Resource and performs any clean-up required to manage the resourceget()Get a Resource from the pool, blocking forever until a resource becomes availableget(long timeout) Get a Resource from the pool, blocking for a specified time until a resource becomes available, a zero timeout blocks forever.<T> List<T>getAllocedOfType(Class<T> type) intGets the number of resources currently assignedintGets the number of resources currently available for assignmentGets the unique identifier for this instancestatic ResourcePoolgetInstance(String key) Gets a registered ResourcePoolintgetMax()Gets the maximum number of resources allowedintgetMin()Gets the minimum number of resourcesintgetSize()Gets the total number of resources createdprotected voidregister()Registers this resource into the common ResourcePoolprotected voidRegisters this resource into the common ResourcePoolstatic voidregister(String key, ResourcePool pool) Registers a resource into the common ResourcePoolvoidRelease a resource back to the pool after its use is completed.voidRemove a resource all together from the pool and dispose of itvoidsetIdentifier(String identifier) Sets the unique identifier for this instancevoidsetMax(int max) Sets the maximum number of resourcesvoidsetMin(int min) Sets the minimum number of resources, this method has no effect on the number of created resources, if the new minimum is less than the old minimum.toString()Returns a string representation of the ResourcePoolprotected abstract booleanAbstract method for validating a resource
-
Field Details
-
freed
The free list -
alloced
The alloced list -
min
protected int minThe minimum # of resources to create at init time -
max
protected int maxThe maximum # of resources allowed -
numCreated
protected int numCreatedThe total number of resources currently created -
identifier
Unique identifier for a resourcePool instance
-
-
Constructor Details
-
ResourcePool
public ResourcePool()
-
-
Method Details
-
create
Abstract method for creating a specific resource in the sub-class- Returns:
- the resource
- Throws:
ResourceUnavailableException- if an error occured in creating the resource
-
dispose
Abstract method for disposing a specific resource in the sub-class- Parameters:
obj- the resource to dispose
-
validate
Abstract method for validating a resource- Parameters:
obj- the resource to validate- Returns:
- true if resource is still valid
-
release
Release a resource back to the pool after its use is completed.- Parameters:
obj- the resource object
-
remove
Remove a resource all together from the pool and dispose of it- Parameters:
obj- the resource object
-
get
Get a Resource from the pool, blocking forever until a resource becomes available- Returns:
- the resource object
- Throws:
ResourceUnavailableException- if an error occured in creating the resource
-
get
Get a Resource from the pool, blocking for a specified time until a resource becomes available, a zero timeout blocks forever.- Parameters:
timeout- the time in milliseconds to wait for a resource to become available- Returns:
- the resource object
- Throws:
ResourceUnavailableException- if an error occured in creating the resourceResourceUnavailableException- if no resource becomes available before the timeout period expires
-
createResource
creates a new Resource and performs any initilization required to manage the resource- Returns:
- the new resource
- Throws:
ResourceUnavailableException
-
allocResource
allocates a Resource and performs any initilization required to manage the alloced resource- Parameters:
resource- the resource to allocate
-
freeResource
frees a Resource and performs any clean-up required to manage the resource- Parameters:
resource- the resource to free
-
disposeResource
disposes of a Resource and performs any clean-up required to manage the resource- Parameters:
resource- the resource to dispose
-
getFreedSize
public int getFreedSize()Gets the number of resources currently available for assignment- Returns:
- the number of resources currently available for assignment
-
getAllocedSize
public int getAllocedSize()Gets the number of resources currently assigned- Returns:
- the number of resources currently assigned
-
getAllocedOfType
-
getSize
public int getSize()Gets the total number of resources created- Returns:
- the total number of resources created
-
getMin
public int getMin()Gets the minimum number of resources- Returns:
- the minimum number of resources
-
getMax
public int getMax()Gets the maximum number of resources allowed- Returns:
- the maximum number of resources allowed
-
setMin
public void setMin(int min) Sets the minimum number of resources, this method has no effect on the number of created resources, if the new minimum is less than the old minimum.- Parameters:
min- the minimum number of resources- Throws:
IllegalArgumentException- if minimum is less than zero or the minimum is is greater than the maximum
-
setIdentifier
Sets the unique identifier for this instance- Parameters:
identifier- the unique identifier for this instance
-
getIdentifier
Gets the unique identifier for this instance- Returns:
- the unique identifier for this instance
-
createResources
Pre-creates the minimum number of resources- Throws:
ResourceUnavailableException
-
createResources
Pre-creates the specified number of resources- Parameters:
number- the number of resources to create- Throws:
ResourceUnavailableException
-
setMax
public void setMax(int max) Sets the maximum number of resources- Parameters:
max- the maximum number of resources- Throws:
IllegalArgumentException- if maximum is less than the minimum
-
getInstance
Gets a registered ResourcePool- Parameters:
key- the identifier for the type of resource pool- Returns:
- the resource pool or null, if one is not registered for the key.
-
register
protected void register()Registers this resource into the common ResourcePool -
register
Registers this resource into the common ResourcePool- Parameters:
key- the identifier for the type of resource pool
-
register
Registers a resource into the common ResourcePool- Parameters:
key- the identifier for the type of resource poolpool- the ResourcePool being registered
-
toString
Returns a string representation of the ResourcePool
-