| java.lang.Object com.tc.util.concurrent.SetOnceFlag
SetOnceFlag | final public class SetOnceFlag implements Serializable(Code) | | A class to model a flag that can be set once and only once If you have a boolean in class that should
really only be set once and only once (like a shutdown flag maybe), using this class might help your class stay
implemented correctly
NOTE: There is purposefully no way to reset the flag. Use of this class is meant to communicate that a particular
flag should be set once and only once (period).
NOTE(2): This class is a lot like a latch in some ways (ie. it can only be set once and not reset). Unlike a latch,
the set() method here is only allowed to be called once (ever). Additionally, there are no wait() style methods on
this flag class
author: teck |
Method Summary | |
public boolean | attemptSet() Attempt to atomically set the flag. | public boolean | isSet() Has the flag been already set? README: This is example of how NOT to use this class:
if (!flag.isSet()) {
flag.set();
// ... | public void | set() | public String | toString() |
SetOnceFlag | public SetOnceFlag()(Code) | | |
SetOnceFlag | public SetOnceFlag(boolean setFlag)(Code) | | Create a new SetOnceFlag, optionally set() 'ing the flag immediately
Parameters: setFlag - true if the flag should be created already set() |
attemptSet | public boolean attemptSet()(Code) | | Attempt to atomically set the flag. This differs from set() in that
it doesn't throw an exception if the value has already been set. This method
is useful for flags that might be set more than once, but should act as a guard
against path(s) that should only ever run once.
NOTE: It is probably almost always wrong to ignore the return value of this method
true iff this invocation actually set the flag, false otherwise |
isSet | public boolean isSet()(Code) | | Has the flag been already set? README: This is example of how NOT to use this class:
if (!flag.isSet()) {
flag.set();
// ... do some protected, assumed one-time actions
}
This example code is broken becuase there is no synchronization and/or checking the return value of
set() . It is certainly possible that two threads could execute the body of the if
block A correct implementation might be:
if (flag.attemptSet()) {
// do one time only actions
} else {
// flag was already set
}
true if the flag is already set |
set | public void set()(Code) | | Attempt to set the flag
true if the flag was set, false otherwise throws: IllegalArgumentException - if the value has already been set by a different thread |
|
|