Class Context
- java.lang.Object
-
- javolution37.javolution.realtime.Context
-
- Direct Known Subclasses:
- ConcurrentContext, HeapContext, LocalContext, LogContext, PoolContext
public abstract class Context extends java.lang.ObjectThis 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 byLocalReferencePoolContext- To transparently reuse objects created using anObjectFactoryinstead 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.loggingprovides class-based logging (based upon class hierarchy).
The scope of a
Contextshould be surrounded by atry, finallyblock 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 voidclear()Clears this context and releases any associated resource.static Contextcurrent()Returns the current context for the current thread.static voidenter(Context context)Enters the specified context.static voidexit(Context context)Exits the specified context.ContextgetOuter()Holds the outer context of this context ornullif none (root context).java.lang.ThreadgetOwner()Returns the current owner of this context.
-
-
-
Method Detail
-
current
public static Context current()
Returns the current context for the current thread. The default context is aHeapContextfor normal threads and aPoolContextforconcurrent 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 ornullif none (root context).- Returns:
- the outer context or
nullif 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.
-
-
DMelt 3.0 © DataMelt by jWork.ORG