org.thymeleaf.util
Class ResourcePool<T>

Object
  extended by org.thymeleaf.util.ResourcePool<T>

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);
   }
 

Since:
2.0.0
Author:
Guven Demir

Nested Class Summary
static interface ResourcePool.IResourceFactory<T>
           Objects implementing this interface are in charge of creating the resources that conform the pool.
 
Constructor Summary
ResourcePool(Collection<T> resources)
           
ResourcePool(ResourcePool.IResourceFactory<T> resourceFactory, int poolSize)
           
 
Method Summary
 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.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourcePool

public ResourcePool(Collection<T> resources)

ResourcePool

public ResourcePool(ResourcePool.IResourceFactory<T> resourceFactory,
                    int poolSize)
Method Detail

allocate

public T allocate()

Allocates and returns a resource from the pool.

Blocks until a resource is available when a resource is not available immediately.

Returns:
the allocated resource, heving been removed from the allocation pool.

release

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.

Parameters:
resource - the resource to be released and returned to the pool.

discardAndReplace

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.

Parameters:
resource - the resource to be discarded and substituted with a new one.
Since:
2.0.7


Copyright © 2013 The THYMELEAF team. All Rights Reserved.