public class PoolContext extends AllocatorContext
This class represents a shared pool context for object
allocation/recycling. Unlike HeapContext
, objects recycled
can be reused by all threads running in a PoolContext
.
// To avoid each new thread to enter a PoolContext, the default
// allocator context can be configured to PoolContext.class
Thread thread1 = new Thread() {
public void run() {
PoolContext.enter();
try {
...
myObject = myObjectFactory.object(); // Returns an object potentially recycled by thread2.
...
} finally {
PoolContext.exit();
}
}};
Thread thread2 = new Thread() {
public void run() {
PoolContext.enter();
try {
...
myObjectFactory.recycle(myObject); // Recycles an object potentially created by thread1.
...
} finally {
PoolContext.exit();
}
}};
AllocatorContext.Reference<T>
DEFAULT
Constructor and Description |
---|
PoolContext()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
deactivate()
Deactivates the
allocators belonging to this context
for the current thread. |
static void |
enter()
Enters a pool context.
|
protected void |
enterAction()
The action to be performed after this context becomes the current
context.
|
static void |
exit()
Exits the current pool context.
|
protected void |
exitAction()
The action to be performed before this context is no more the current
context.
|
protected Allocator |
getAllocator(ObjectFactory factory)
Returns the allocator for the specified factory in this context.
|
getCurrentAllocatorContext, getDefault, outerCopy, outerCopy, outerExecute
enter, enter, exit, exit, getCurrentContext, getOuter, getOwner, setConcurrentContext, toString
public static void enter()
public static void exit()
java.lang.ClassCastException
- if the context is not a pool context.protected void deactivate()
AllocatorContext
allocators
belonging to this context
for the current thread. This method is typically called when an inner
allocator context is entered by the current thread, when exiting an
allocator context or when a concurrent executor has completed its task
within this allocator context. Deactivated allocators have no
user
(null
).deactivate
in class AllocatorContext
protected Allocator getAllocator(ObjectFactory factory)
AllocatorContext
getAllocator
in class AllocatorContext
factory
- the factory for which the allocator is returned.protected void enterAction()
Context
enterAction
in class Context
protected void exitAction()
Context
exitAction
in class Context
Copyright © 2005 - 2007 Javolution.