Skip to content

Add getAndUpdate() method to AtomicReference #91

Closed

Description

@volo

On JVM the AtomicReference class contains the getAndUpdate() method. It'd be convenient to have it for other targets/common code as well. The non-JVM implementation can look like:

fun getAndUpdate(updateFunction: (V) -> V): V {
    while(true) {
        val prev = get()
        val next = updateFunction(prev)
        if (compareAndSet(prev, next)) return prev
    }
}

The signature of the corresponding JVM method is:

public final V getAndUpdate(UnaryOperator<V> updateFunction)

where UnaryOperator is a functional interface:

@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T>

That should place nicely with the SAM conversion

Activity

  1. locked and limited conversation to collaborators on Aug 7, 2026
  2. converted this issue into a discussion #122 on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions