public final class ImmortalContext extends AllocatorContext
This class represents an allocator from immortal memory (RTSJ).
It is typically used to allocate (and recycle) from immortal memory
allowing dynamically created static instances to be accessible by
NoHeapRealtimeThread
:
public synchronized Text intern() {
if (!INTERN_INSTANCES.containsKey(this)) {
ImmortalContext.enter();
try { // Forces interned instance to be in immortal memory.
Text txt = this.copy(); // In ImmortalMemory.
INTERN_INSTANCES.put(txt, txt);
} finally {
ImmortalContext.exit();
}
}
return (Text) INTERN_INSTANCES.get(str);
}
Because class initialization may occur while running in a non-heap
context, it is recommended to force factory produced constants
to immortal memory:
public class Rational {
public static final Rational ZERO;
public static final Rational ONE;
...
static { // Forces constants to ImmortalMemory.
ImmortalContext.enter();
try {
ZERO = Rational.valueOf(0, 1); // Factory produced.
ONE = Rational.valueOf(1, 1); // Factory produced.
} finally {
ImmortalContext.exit();
}
}
}
AllocatorContext.Reference<T>
DEFAULT
Modifier and Type | Method and Description |
---|---|
protected void |
deactivate()
Deactivates the
allocators belonging to this context
for the current thread. |
static void |
enter()
Enters an immortal memory context.
|
protected void |
enterAction()
The action to be performed after this context becomes the current
context.
|
static void |
exit()
Exits the current immortal memory 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 an immortal 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.