AtomicInteger.incrementAndGet() vs. AtomicInteger.getAndIncrement()
Solution 1
Since no answer to the actual question has been given, here's my personal opinion based on the other answers (thanks, upvoted) and Java convention:
incrementAndGet()
is better, because method names should start with the verb describing the action, and intended action here is to increment only.
Starting with verb is the common Java convention, also described by official docs:
Solution 2
The code is essentially the same so it does not matter:
public final int getAndIncrement() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return current;
}
}
public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
Solution 3
No, there's no difference (if you don't care about the return value).
The code of those methods (in the OpenJDK) differs only in that one uses return next
and the other uses return current
.
Both use compareAndSet
under the hood with the exact same algorithm. Both need to know both the old and the new value.
Solution 4
Just want to add to existing answers: there could be very small non-noticeable difference.
If you look at this implementation:
public final int getAndIncrement() {
return unsafe.getAndAddInt(this, valueOffset, 1);
}
public final int incrementAndGet() {
return unsafe.getAndAddInt(this, valueOffset, 1) + 1;
}
Note - both function call exactly the same function getAndAddInt
, except +1
part, which means that in this implementation getAndIncrement
is faster.
But, here is older implementation:
public final int getAndIncrement() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return current;
}
}
public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
The only difference is return variable, so both functions perform exactly the same.
Comments
-
hyde almost 2 years
When return value is not of interest, is there any (even irrelevant in practice) difference between
AtomicInteger.getAndIncrement()
andAtomicInteger.incrementAndGet()
methods, when return value is ignored?I'm thinking of differences like which would be more idiomatic, as well as which would put less load in CPU caches getting synchronized, or anything else really, anything to help decide which one to use more rationally than tossing a coin.
-
irreputable about 11 years
incrementAndGet()==getAndIncrement()+1
-
assylias about 11 years@irreputable Not sure I get your point - if you don't store the returned value,
var.incrementAndGet();
andvar.getAndIncrement();
produce exactly the same result... -
irreputable about 11 yearsyes. I'm saying actually one of the methods is enough, the other is kind of redundant.
-
assylias about 11 years@irreputable Ah sorry... Yes. Also I'm not sure why
incrementAndGet
does not simply calladdAndGet(1)
instead of mostly duplicating the code... -
irreputable about 11 yearsDoug Lea is obsessed with every CPU cycle:)
-
assylias about 11 years@irreputable Following-up on that in this question.
-
hyde almost 7 yearsThanks for your answer. However, in your answer, the result is actually used (printed). The question is about a situation where "When return value is not of interest"...
-
Bennett Lynch about 6 yearsWould be nice if they exposed a simple
increment()
method, just for the sake of readability. -
Delark about 2 yearsI guess the reason why they kept it like that in atomicInteger() is to "maintain the convention" as the behaviour of returning either "prev" or "next" becomes a necessity when dealing with atomic swaps. This is not useful in integers since the next will always be prev + 1 (and prev == next -1), but when dealing with objects it is extremely useful. More problematic to me is the fact that a failed compareAndSet does not give you a witness.