J avolution v5.2 (J2SE 1.5+)

javolution.context
Class ImmortalContext

java.lang.Object
  extended by javolution.context.Context
      extended by javolution.context.AllocatorContext
          extended by javolution.context.ImmortalContext
All Implemented Interfaces:
java.io.Serializable, XMLSerializable

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 (e.g. StackContext), 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();
                 } 
             }
        }

Version:
5.2, August 19, 2007
Author:
Jean-Marie Dautelle
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javolution.context.AllocatorContext
AllocatorContext.Reference<T>
 
Field Summary
 
Fields inherited from class javolution.context.AllocatorContext
DEFAULT
 
Fields inherited from class javolution.context.Context
ROOT
 
Method Summary
protected  void deactivate()
          Deactivates the allocators belonging to this context for the current thread.
static ImmortalContext enter()
          Enters an immortal memory context.
protected  void enterAction()
          The action to be performed after this context becomes the current context.
static ImmortalContext 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.
 
Methods inherited from class javolution.context.AllocatorContext
getCurrent, getDefault
 
Methods inherited from class javolution.context.Context
enter, enter, exit, getOuter, getOwner, setCurrent, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

enter

public static ImmortalContext enter()
Enters an immortal memory context.

Returns:
the immortal memory context entered.

exit

public static ImmortalContext exit()
Exits the current immortal memory context.

Returns:
the immortal context being exited.
Throws:
java.lang.ClassCastException - if the context is not an immortal context.

deactivate

protected void deactivate()
Description copied from class: AllocatorContext
Deactivates the 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).

Specified by:
deactivate in class AllocatorContext

getAllocator

protected Allocator getAllocator(ObjectFactory factory)
Description copied from class: AllocatorContext
Returns the allocator for the specified factory in this context.

Specified by:
getAllocator in class AllocatorContext
Parameters:
factory - the factory for which the allocator is returned.
Returns:
the allocator producing instances of the specified factory.

enterAction

protected void enterAction()
Description copied from class: Context
The action to be performed after this context becomes the current context.

Specified by:
enterAction in class Context

exitAction

protected void exitAction()
Description copied from class: Context
The action to be performed before this context is no more the current context.

Specified by:
exitAction in class Context

J avolution v5.2 (J2SE 1.5+)

Copyright © 2005 - 2007 Javolution.