Quick revision
The short answer
updateAndGet(f) and accumulateAndGet(x, f) apply a function to the current value of an AtomicInteger atomically, by reading the value, computing the new one, and retrying a compare-and-set until no other thread changed the value in between. updateAndGet takes a function of the current value only, while accumulateAndGet combines the current value with an argument we pass. Both return the new value (getAndUpdate and getAndAccumulate return the old one), and because the function may run more than once under contention, it must be side-effect free and quick.
What to remember
In this answer, we saw that updateAndGet and accumulateAndGet apply a function atomically by retrying a compare-and-set until it succeeds. updateAndGet works on the current value alone, accumulateAndGet combines it with an argument, and the get-first variants return the old value instead of the new. Because the loop may call the function several times under contention, the function must be pure and quick, and side effects belong after the call, using the value it returns.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
updateAndGet(f) and accumulateAndGet(x, f) apply a function to the current value of an AtomicInteger atomically, by reading the value, computing the new one, and retrying a compare-and-set until no other thread changed the value in between. updateAndGet takes a function of the current value only, while accumulateAndGet combines the current value with an argument we pass. Both return the new value (getAndUpdate and getAndAccumulate return the old one), and because the function may run more than once under contention, it must be side-effect free and quick.
Why this question is asked
Most of us know incrementAndGet, but real counters often need more than adding one: capping at a maximum, tracking a running maximum, or wrapping around at a limit. Before Java 8 we wrote a compare-and-set loop by hand for each of these. updateAndGet and accumulateAndGet package that loop, and interviewers ask about them because the package hides an important rule that people break. In this answer, we will recap compare-and-set, see what each method does with examples, look inside the retry loop to understand why the function can run several times, and then see what goes wrong when the function has side effects or is slow.
1A Quick Recap of Compare-and-Set
An AtomicInteger holds an int that many threads can change safely without a lock. Its basic tool is compareAndSet(expected, newValue), often called CAS: it changes the value to newValue only if it is still equal to expected, and it does that check and write as one indivisible step. It returns true if it wrote and false if another thread changed the value first.
Any update can be built on CAS: read the current value, compute the new one, try to CAS from what we read to what we computed, and if that fails, read again and repeat. This is called a CAS loop, and it is the idea behind both methods in this question.
2UpdateAndGet: A Function of the Current Value
updateAndGet takes an IntUnaryOperator, a function from one int to another. It applies the function to the current value and stores the result atomically. Here are a few common uses.
Each of these would be a race if written as get() followed by set(), because another thread could change the value between the two calls. With updateAndGet the whole read-compute-write is atomic from the point of view of other threads.
3AccumulateAndGet: Combining With an Argument
accumulateAndGet(x, f) takes a value x and an IntBinaryOperator, a function of two ints. It calls f(current, x) and stores the result. It exists so that one function object, often a method reference, can be reused with different arguments instead of building a new lambda that captures x on every call.
| Method | Function | Returns |
|---|---|---|
updateAndGet(f) | f(current) | new value |
getAndUpdate(f) | f(current) | old value |
accumulateAndGet(x, f) | f(current, x) | new value |
getAndAccumulate(x, f) | f(current, x) | old value |
The order of arguments matters for functions that are not symmetric: the current value is always the first argument and our x is the second. Math::max does not care, but a function such as (cur, x) -> cur - x does.
4Inside the Retry Loop
The JDK implements these methods with a CAS loop. Simplified, it looks like the code below. The real code also avoids calling the function again when a weak CAS fails spuriously while the value did not actually change, but the idea is the same.
- Read current valueprev = 10
- Apply the functionnext =
f(10)= 20 - CAS from prev to next succeeds?YesReturn nextNoValue changed; read again and reapply f
3 steps in order. Play them, or step through them one at a time.
Notice what happens under contention. Thread 1 reads 10 and computes 20. Before it can CAS, thread 2 changes the value to 11. Thread 1's CAS from 10 to 20 fails, so it reads 11 and calls the function again, computing 22. The function ran twice for one successful update. With many threads updating the same value, it can run many times. That is the reason for the rule.
5Why the Function Must Be Side-Effect Free
Because the function can run more than once, anything it does besides computing a result can happen more than once too. Sending a message, incrementing another counter, writing a log line or adding to a list all become duplicated under contention, and they happen for results that were thrown away.
The function should also not depend on mutable state outside it, because it must give the same answer for the same input when retried. A pure function of its arguments is the safe rule.
Speed matters too. The longer the function takes, the wider the window between the read and the CAS, and the more likely another thread changes the value in that window, forcing a retry, which takes just as long again. A function that does a hash map lookup or parsing can turn a cheap update into a retry storm under load. If the computation is expensive, it is better to compute outside and use a simple CAS, or to use a lock.
6Follow-up questions
Can the function be called even when there is no contention?
In the common case it is called once. On some hardware a weak compare-and-set can fail spuriously; the JDK code rechecks the value and, if it is unchanged, retries without calling the function again. So without contention we can expect a single call, but correctness must never depend on that.
Is updateAndGet slower than incrementAndGet?
Usually a little. incrementAndGet maps to a single atomic add instruction on x86 with no retry loop, while updateAndGet needs a read, a function call and a CAS that can fail. For plain addition, use addAndGet or incrementAndGet; use updateAndGet when the update is not a simple sum.
Do the same methods exist on other atomic classes?
Yes. AtomicLong has the long versions, and AtomicReference has updateAndGet and accumulateAndGet with UnaryOperator and BinaryOperator, which is how we update an immutable object atomically. The same side-effect rule applies to all of them.
7Conclusion
In this answer, we saw that updateAndGet and accumulateAndGet apply a function atomically by retrying a compare-and-set until it succeeds. updateAndGet works on the current value alone, accumulateAndGet combines it with an argument, and the get-first variants return the old value instead of the new. Because the loop may call the function several times under contention, the function must be pure and quick, and side effects belong after the call, using the value it returns.
Open with
Both methods apply a function to the AtomicInteger atomically. Internally they're a compare-and-set loop: read the current value, compute the new value with my function, and try to CAS from the old to the new; if another thread changed it in between, the CAS fails and the loop reads again and reapplies the function. updateAndGet takes a unary function of the current value, so I'd use it for things like capping, x to min of x plus one and a limit, or wrapping around. accumulateAndGet takes an extra argument and a binary function called with the current value first and my argument second, so tracking a maximum is accumulateAndGet of latency with Math max.
Then, if they want more
Both return the new value; getAndUpdate and getAndAccumulate return the value before the update. The rule that matters is that the function can run more than once under contention, so it must be side-effect free: no logging, no sending messages, no adding to collections inside it, otherwise those effects get duplicated for attempts that were thrown away.
I'd do side effects after the call, using the returned value. And the function should be quick, because a slow function widens the window between the read and the CAS, which makes retries more likely, and each retry costs the full function again.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What do updateAndGet and accumulateAndGet do on AtomicInteger, and what rule must the...": https://scrivio-interview-library.netlify.app/java/atomics/0b896e9195/