Package org.apache.shiro.util
Interface ThreadState
-
- All Known Implementing Classes:
SubjectThreadState
public interface ThreadState
AThreadState
instance manages any state that might need to be bound and/or restored during a thread's execution.Usage
Callingbind()
will place state on the currently executing thread to be accessed later during the thread's execution.WARNING
After the thread is finished executing, or if an exception occurs, any previous state MUST berestored
to guarantee all threads stay clean in any thread-pooled environment. This should always be done in atry/finally
block:ThreadState state = //acquire or instantiate as necessary try { state.bind(); doSomething(); //execute any logic downstream logic that might need to access the state } finally { state.restore(); }
- Since:
- 1.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
bind()
Binds any state that should be made accessible during a thread's execution.void
clear()
Completely clears/removes theThreadContext
state.void
restore()
Restores a thread to its state before bindbind
was invoked.
-
-
-
Method Detail
-
bind
void bind()
Binds any state that should be made accessible during a thread's execution. This should typically always be called in atry/finally
block paired with therestore()
call to guarantee that the thread is cleanly restored back to its original state. For example:ThreadState state = //acquire or instantiate as necessary try { state.bind(); doSomething(); //execute any logic downstream logic that might need to access the state } finally { state.restore(); }
-
restore
void restore()
Restores a thread to its state before bindbind
was invoked. This should typically always be called in afinally
block to guarantee that the thread is cleanly restored back to its original state beforebind
's bind was called. For example:ThreadState state = //acquire or instantiate as necessary try { state.bind(); doSomething(); //execute any logic downstream logic that might need to access the state } finally { state.restore(); }
-
clear
void clear()
Completely clears/removes theThreadContext
state. Typically this method should only be called in special cases - it is more 'correct' torestore
a thread to its previous state than to clear it entirely.
-
-