Documentation of 'javolution37.javolution.realtime.Context' Java class
Context
javolution37.javolution.realtime

Class Context

  • Direct Known Subclasses:
    ConcurrentContext, HeapContext, LocalContext, LogContext, PoolContext


    public abstract class Context
    extends java.lang.Object

    This class represents a real-time context; they are typically thread-local but they can also be associated to particular objects.

    This package provides few predefined contexts:

    • LocalContext - To define locally scoped setting held by LocalReference
    • PoolContext - To transparently reuse objects created using an ObjectFactory instead of a constructor.
    • ConcurrentContext - To take advantage of concurrent algorithms on multi-processors systems.
    • LogContext - For thread-based or object-based logging capability. Note: java.util.logging provides class-based logging (based upon class hierarchy).
    Context-aware applications may extend the context base class or any predefined contexts in order to facilitate separation of concern (e.g. logging, security, performance and so forth).

    The scope of a Context should be surrounded by a try, finally block statement to ensure correct behavior in case of exceptions being raised. For example:

         LocalContext.enter();
         try 
             Length.showAs(Unit.FOOT); // Thread-Local setting (no impact on other threads)
             ... 
         } finally {
             LocalContext.exit();
         }
         
         public class Calculator {  
             private PoolContext _pool = new PoolContext();
             public void execute(Runnable logic) {
                 PoolContext.enter(_pool); 
                 try {
                     logic.run(); // Executes the logic using this calculator pool.
                 } finally {
                     PoolContext.exit(_pool); // Recycles used objects all at once.    
                 }
             }
         }

    Finally, context instances can be serialized/deserialized in xml format. For example, applications may want to load pool contexts at start-up to limit the number of object creation at run-time (and therefore reducing the worst-case execution time).

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      void clear()
      Clears this context and releases any associated resource.
      static Context current()
      Returns the current context for the current thread.
      static void enter(Context context)
      Enters the specified context.
      static void exit(Context context)
      Exits the specified context.
      Context getOuter()
      Holds the outer context of this context or null if none (root context).
      java.lang.Thread getOwner()
      Returns the current owner of this context.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • current

        public static Context current()
        Returns the current context for the current thread. The default context is a HeapContext for normal threads and a PoolContext for concurrent threads.
        Returns:
        the current context (always different from null).
      • getOwner

        public final java.lang.Thread getOwner()
        Returns the current owner of this context. The owner of a context is the thread which entered the context.
        Returns:
        the thread owner of this context.
      • getOuter

        public final Context getOuter()
        Holds the outer context of this context or null if none (root context).
        Returns:
        the outer context or null if none (root context).
      • clear

        public void clear()
        Clears this context and releases any associated resource. Dead threads contexts are automatically cleared before finalization.
      • enter

        public static void enter(Context context)
        Enters the specified context.
        Parameters:
        context - the context being entered.
        Throws:
        java.lang.IllegalStateException - if this context is currently in use.
      • exit

        public static void exit(Context context)
        Exits the specified context. The outer context becomes the current context.
        Parameters:
        context - the context being entered.
        Throws:
        java.lang.IllegalStateException - if this context is not the current context.

DMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.