Java AtomicLong addAndGet(AtomicLong current, long toAdd)

Here you can find the source of addAndGet(AtomicLong current, long toAdd)

Description

Concurrent addition bound to Long.MAX_VALUE.

License

Open Source License

Parameter

Parameter Description
current current atomic to update
toAdd delta to add

Return

Addition result or Long.MAX_VALUE

Declaration

public static long addAndGet(AtomicLong current, long toAdd) 

Method Source Code

//package com.java2s;
/*/* w  w  w .  jav  a 2s .co  m*/
 * Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;

public class Main {
    /**
     * Concurrent addition bound to Long.MAX_VALUE.
     * Any concurrent write will "happen" before this operation.
     *
     * @param current current atomic to update
     * @param toAdd   delta to add
     * @return Addition result or Long.MAX_VALUE
     */
    public static long addAndGet(AtomicLong current, long toAdd) {
        long u, r;
        do {
            r = current.get();
            if (r == Long.MAX_VALUE) {
                return Long.MAX_VALUE;
            }
            u = addCap(r, toAdd);
        } while (!current.compareAndSet(r, u));

        return u;
    }

    /**
     * Concurrent addition bound to Long.MAX_VALUE.
     * Any concurrent write will "happen" before this operation.
     *
     * @param updater  current field updater
     * @param instance current instance to update
     * @param n    delta to add
     * @return Addition result or Long.MAX_VALUE
     */
    public static <T> long addAndGet(AtomicLongFieldUpdater<T> updater,
            T instance, long n) {
        for (;;) {
            long r = updater.get(instance);
            if (r == Long.MAX_VALUE) {
                return Long.MAX_VALUE;
            }
            long u = addCap(r, n);
            if (updater.compareAndSet(instance, r, u)) {
                return r;
            }
        }
    }

    /**
     * Cap an addition to Long.MAX_VALUE
     *
     * @param a left operand
     * @param b right operand
     * @return Addition result or Long.MAX_VALUE if overflow
     */
    public static long addCap(long a, long b) {
        long res = a + b;
        if (res < 0L) {
            return Long.MAX_VALUE;
        }
        return res;
    }
}

Related

  1. add(AtomicLong requested, long n)
  2. add(AtomicLongFieldUpdater updater, T instance, long n)
  3. addstat(Map stat, String key)
  4. average(Stream stream)
  5. compareAndSetIfGreater(final AtomicLong dest, final long tryValue)
  6. createAtomicId()