Package io.camunda.zeebe.util
Class AtomicUtil
java.lang.Object
io.camunda.zeebe.util.AtomicUtil
Utility class for common tasks with
AtomicReference instances.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Treplace(AtomicReference<T> ref, Function<T, Optional<T>> replacer, Consumer<T> rollback) Locklessly replaces the value of an atomic reference using the provided replacer function and rollbacks if the value was replaced concurrently.
-
Method Details
-
replace
public static <T> T replace(AtomicReference<T> ref, Function<T, Optional<T>> replacer, Consumer<T> rollback) Locklessly replaces the value of an atomic reference using the provided replacer function and rollbacks if the value was replaced concurrently.If the replacer function returns an empty optional, the value of the atomic reference is not replaced.
If the value of the atomic reference has been replaced by another thread in the meantime, the replacer function is called again. Additionally, the rollback function is applied to any non-empty value returned by the previous replacer function call.
- Type Parameters:
T- The type of the value of the atomic reference- Parameters:
ref- The atomic reference that holds the value to replacereplacer- The replacer function used to provide a new value to the atomic reference; ifemptythe value of atomic reference is not replacedrollback- The rollback function used to revert any side effect produced by the replacer function- Returns:
- The previous value of the atomic reference, or null if the value was not replaced
-