Java Reference
In-Depth Information
Because of its fragility, ad-hoc thread confinement should be used sparingly; if possible, use
one of the stronger forms of thread confinment (stack confinement or ThreadLocal ) in-
stead.
3.3.2. Stack Confinement
Stack confinement is a special case of thread confinement in which an object can only be
reached through local variables. Just as encapsulation can make it easier to preserve invari-
ants, local variables can make it easier to confine objects to a thread. Local variables are in-
trinsically confined to the executing thread; they exist on the executing thread's stack, which
is not accessible to other threads. Stack confinement (also called within-thread or thread-loc-
al usage, but not to be confused with the ThreadLocal library class) is simpler to maintain
and less fragile than ad-hoc thread confinement.
For primitively typed local variables, such as numPairs in loadTheArk in Listing 3.9 ,
you cannot violate stack confinement even if you tried. There is no way to obtain a reference
to a primitive variable, so the language semantics ensure that primitive local variables are al-
ways stack confined.
Listing 3.9. Thread Confinement of Local Primitive and Reference Variables.
Search WWH ::




Custom Search