Package com.nt.udc.util.state
Class StateFreezeHelper
java.lang.Object
com.nt.udc.util.state.StateFreezeHelper
This class is intended for use within a class that has implemented
the StateFreezable interface. It encapsulates some of the key
state freezing behaviors, specifically the relationship
between the freezing of the main processing, and the
unfreezing by the unfreeze() method.
To use this class, several of the methods must be called in the appropriate places in the methods of the StateFreezable interface.
"freeze()" should be used in the implementation of StateFreezable.freezeState(). It will cause any call to "checkFreezeAndWait()" to halt within that method, effectively stopping that Thread.
"checkFreezeAndWait()" should be used in the main processing thread that one wishes to freeze. At the appropriate place, call this method, and it will halt the thread if "freeze()" has been called previously. Otherwise, it will return, and the prcessing thread will process normally. It will halt in this method until the "unfreeze()" method is called, preferably in the StateFreezable.unfreezeState() method.
"unfreeze()" should be used in the implementation of StateFreezble.unfreezeState(). It is responsible for resuming the thread previously halted in the "checkFreezeAndWait()" method.
Note that the implementation of "checkFreezeAndWait()" uses Object.wait(), and "unfreeze()" uses Object.notify().
To use this class, several of the methods must be called in the appropriate places in the methods of the StateFreezable interface.
"freeze()" should be used in the implementation of StateFreezable.freezeState(). It will cause any call to "checkFreezeAndWait()" to halt within that method, effectively stopping that Thread.
"checkFreezeAndWait()" should be used in the main processing thread that one wishes to freeze. At the appropriate place, call this method, and it will halt the thread if "freeze()" has been called previously. Otherwise, it will return, and the prcessing thread will process normally. It will halt in this method until the "unfreeze()" method is called, preferably in the StateFreezable.unfreezeState() method.
"unfreeze()" should be used in the implementation of StateFreezble.unfreezeState(). It is responsible for resuming the thread previously halted in the "checkFreezeAndWait()" method.
Note that the implementation of "checkFreezeAndWait()" uses Object.wait(), and "unfreeze()" uses Object.notify().
-
Constructor Summary
ConstructorsConstructorDescriptionStateFreezeHelper
(LoggerIfc logger) Constructs the StateFreezeHelper with a logger for unexpected errors. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Checks if the StateFreezeHelper's state is frozen, and halts the current thread if it is.void
Changes the StateFreezeHelper to a "frozen" state.void
unfreeze()
Changes the StateFreezeHelper to an "unfrozen" state, and resumes all processing previously halted in "checkFreezeAndWait()".
-
Constructor Details
-
StateFreezeHelper
Constructs the StateFreezeHelper with a logger for unexpected errors.- Parameters:
logger
- Logger for errors.
-
-
Method Details
-
freeze
Changes the StateFreezeHelper to a "frozen" state. All calls to "checkFreezeAndWait()" will halt in that method when this method has been called.
The Callback should be the Callback received from StateFreezable.freezeState().- Parameters:
freezeCompleted
- The callback to notify the StateManager that the processing has been frozen.
-
unfreeze
public void unfreeze()Changes the StateFreezeHelper to an "unfrozen" state, and resumes all processing previously halted in "checkFreezeAndWait()".
Note: This method uses Object.notify() as part of its implementation. -
checkFreezeAndWait
public void checkFreezeAndWait()Checks if the StateFreezeHelper's state is frozen, and halts the current thread if it is. The StateFreezeHelper is considered "frozen" if "freeze()" has been called previously. If "frozen", the current thread will be halted until "unfreeze()" is called on a different thread. Once "unfreeze()" has been called, processing resumes normally on the current thread.
Note: This method uses Object.wait() as part of its implementation.
-