public final class ResourcePool<T> extends Object
Represents a limited set or pool of resources, which needs to be used with exclusive ownership.
Should be used in a similar fashion to the following so that resources are always released properly:
MyResource resource = myLimitedResource.allocate(); try { // actual code using the resource } finally { myLimitedResource.release(resource); }
Modifier and Type | Class and Description |
---|---|
static interface |
ResourcePool.IResourceFactory<T>
Objects implementing this interface are in charge of
creating the resources that conform the pool.
|
Constructor and Description |
---|
ResourcePool(Collection<T> resources) |
ResourcePool(ResourcePool.IResourceFactory<T> resourceFactory,
int poolSize) |
Modifier and Type | Method and Description |
---|---|
T |
allocate()
Allocates and returns a resource from the pool.
|
void |
discardAndReplace(T resource)
Discards an allocated resource and forces the resource
factory to create a new one, if a resource factory has been
specified during pool instantiation.
|
void |
release(T resource)
Releases a previously allocated resource.
|
public ResourcePool(Collection<T> resources)
public ResourcePool(ResourcePool.IResourceFactory<T> resourceFactory, int poolSize)
public T allocate()
Allocates and returns a resource from the pool.
Blocks until a resource is available when a resource is not available immediately.
public void release(T resource)
Releases a previously allocated resource.
Might also be used to introduce new resources, e.g. in place of a broken resource.
resource
- the resource to be released and returned to the pool.public void discardAndReplace(T resource)
Discards an allocated resource and forces the resource factory to create a new one, if a resource factory has been specified during pool instantiation.
If a resource factory has not been specified, this method will raise an IllegalStateException.
resource
- the resource to be discarded and substituted with a
new one.Copyright © 2017 The THYMELEAF team. All rights reserved.