Package org.nanonative.nano.helper
Class LockedBoolean
java.lang.Object
org.nanonative.nano.helper.LockedBoolean
A thread-safe class that manages a boolean state with synchronized actions.
Unlike
AtomicBoolean, LockedBoolean allows executing complex
synchronized actions based on the current state and conditionally updating the state within a locked context.
This class is designed for scenarios where actions need to be performed during state changes,
ensuring thread safety and atomicity of operations.-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new LockedBoolean with the initial state set to false.LockedBoolean(boolean state) Constructs a new LockedBoolean with the specified initial state. -
Method Summary
Modifier and TypeMethodDescriptionbooleanget()Retrieves the current state with thread safety.voidExecutes aConsumerbased on a condition without changing the state.voidExecutes aConsumerbased on the current state.voidset(boolean state) Sets the state to the specified value with thread safety.voidset(boolean when, boolean then) Conditionally sets the state based on a condition.voidConditionally sets the state and executes aConsumerbased on a condition.voidSets the state to the specified value with thread safety.toString()
-
Constructor Details
-
LockedBoolean
public LockedBoolean()Constructs a new LockedBoolean with the initial state set to false. -
LockedBoolean
public LockedBoolean(boolean state) Constructs a new LockedBoolean with the specified initial state.- Parameters:
state- the initial state
-
-
Method Details
-
set
public void set(boolean state) Sets the state to the specified value with thread safety.- Parameters:
state- the new state value
-
set
Sets the state to the specified value with thread safety.- Parameters:
state- the new state valuerun- TheConsumerto execute while setting new state.
-
set
public void set(boolean when, boolean then) Conditionally sets the state based on a condition.- Parameters:
when- The condition that determines if the state should be set tothen.then- The state to set ifwhenis true.
-
set
Conditionally sets the state and executes aConsumerbased on a condition.- Parameters:
when- The condition that determines if the state should be set tothen.then- The state to set ifwhenis true.run- TheConsumerto execute if the conditionwhenis true.
-
run
Executes aConsumerbased on the current state.- Parameters:
run- The Runnable to execute if the current state is true.
-
run
Executes aConsumerbased on a condition without changing the state.- Parameters:
when- The condition that determines which Runnable to execute.then- TheConsumerto execute ifwhenis true.
-
get
public boolean get()Retrieves the current state with thread safety.- Returns:
- the current boolean state
-
toString
-