Poolable objects.
This is where the objects in the Pool comes from. Clients of the Stormpot
library needs to provide their own Allocator implementations.
Implementations of this interface must be thread-safe, because there is no
knowing whether pools will try to access it concurrently or not. The easiest
way to achieve this is to just make the
allocate
and deallocate methods synchronised,
but this might cause problems for the pools ability to allocate objects fast
enough, if they are very slow to construct. Pools might want to deal with
very slow allocations by allocating in more than one thread, but
synchronizing the allocate(Slot) method will render that strategy
ineffective.
A better approach to thread-safety is to not have any shared mutable state
in the allocator, if at all possible.
Implementations must also be interruption-safe.
When a pool is shut down, its background
allocation thread may be interrupted, and this thread may be interacting
with the allocator instance when this happens.
stormpot.Reallocator
| Modifier and Type | Method and Description |
|---|---|
| public T | Returns: A newly created instance of T. Never `null`.The slot the pool wish to allocate an object for.
Implementers do not need to concern themselves with the details of a
pools slot objects. They just have to call release on them as the
protocol demands. slot)Create a fresh new instance of T for the given slot. |
| public void | deallocate(T
The non-null Poolable instance to be deallocated. poolable)Deallocate, if applicable, the given Poolable and free any resources associated with it. |
| allocate | back to summary |
|---|---|
| public T allocate(Slot slot) throws Exception Create a fresh new instance of T for the given slot.
The returned
| |
| deallocate | back to summary |
|---|---|
| public void deallocate(T poolable) throws Exception Deallocate, if applicable, the given Poolable and free any resources
associated with it.
This is an opportunity to close any connections or files, flush buffers,
empty caches or what ever might need to be done to completely free any
resources represented by this Poolable.
Note that a Poolable must never touch its slot object after it has been
deallocated.
Pools, on the other hand, will guarantee that the same object is never
deallocated more than once.
Note that pools will always silently swallow exceptions thrown by the
deallocate method. They do this because there is no knowing whether the
deallocation of an object will be done synchronously by a thread calling
| |