J avolution v5.4 (J2SE 1.6+)

javolution.context
Class HeapContext

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

public class HeapContext
extends AllocatorContext

This class represents the default allocator context. Allocations are performed using the new keyword and explicit object recycling is supported:

         char[] buffer = ArrayFactory.CHARS_FACTORY.array(4098); // Possibly recycled.
         while (reader.read(buffer) > 0) { ... }
         ArrayFactory.CHARS_FACTORY.recycle(buffer); // Explicit recycling.
     

It should be noted that object recycling is performed on a thread basis (for performance reasons) and should only be performed if the object has been factory produced by the same thread doing the recycling. It is usually not a problem because recycling is done for temporary objects within the same method. For example:

     public String toString() {
         TextBuilder tmp = TextBuilder.newInstance(); // Calls ObjectFactory.object()
         try {
             tmp.append(...);
             ...
             return tmp.toString();
         } finally {
             TextBuilder.recycle(tmp); // Calls ObjectFactory.recycle(...)
         }
     }
If allocation/recycling is performed by different threads then PoolContext should be employed.

Version:
5.3, March 19, 2009
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
 
Constructor Summary
HeapContext()
          Default constructor.
 
Method Summary
protected  void deactivate()
          Deactivates the allocators belonging to this context for the current thread.
static void enter()
          Enters a heap context.
protected  void enterAction()
          The action to be performed after this context becomes the current context.
static void exit()
          Exits the current heap 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, outerCopy, outerCopy, outerExecute
 
Methods inherited from class javolution.context.Context
enter, enter, exit, exit, getOuter, getOwner, setCurrent, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HeapContext

public HeapContext()
Default constructor.

Method Detail

enter

public static void enter()
Enters a heap context.


exit

public static void exit()
Exits the current heap context.

Throws:
java.lang.ClassCastException - if the context is not a heap 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.4 (J2SE 1.6+)

Copyright © 2005 - 2009 Javolution.