[concurrency-interest] AtomicInteger implementation

Yann Le Tallec ylt at letallec.org
Thu Feb 28 16:29:34 EST 2013


Hello,

all the code of incrementAndGet (in JDK 7) but one line is identical to
addAndGet.
What is the reason why the code has been duplicated instead of implementing
incrementAndGet as:

public final int incrementAndGet() {
    return addAndGet(1);}

Regards,
Yann

-----

For reference, the two methods:

public final int addAndGet(int delta) {
    for (;;) {
        int current = get();
        int next = current + delta;         // Only difference
        if (compareAndSet(current, next))
            return next;
    }}
public final int incrementAndGet() {
    for (;;) {
        int current = get();
        int next = current + 1;             // Only difference
        if (compareAndSet(current, next))
            return next;
    }}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20130228/2cc8ce28/attachment.html>


More information about the Concurrency-interest mailing list