Class AtomicUtil

java.lang.Object
io.camunda.zeebe.util.AtomicUtil

public final class AtomicUtil extends Object
Utility class for common tasks with AtomicReference instances.
  • 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 replace
      replacer - The replacer function used to provide a new value to the atomic reference; if empty the value of atomic reference is not replaced
      rollback - 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