Java AtomicLong getAndAddRequest(AtomicLongFieldUpdater requested, T object, long n)

Here you can find the source of getAndAddRequest(AtomicLongFieldUpdater requested, T object, long n)

Description

Adds n to requested field and returns the value prior to addition once the addition is successful (uses CAS semantics).

License

Apache License

Parameter

Parameter Description
requested atomic field updater for a request count
object contains the field updated by the updater
n the number of requests to add to the requested count

Return

requested value just prior to successful addition

Declaration

public static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> requested, T object, long n) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**/*from   w  ww. ja v a2  s .  co  m*/
     * Adds {@code n} to {@code requested} field and returns the value prior to
     * addition once the addition is successful (uses CAS semantics). If
     * overflows then sets {@code requested} field to {@code Long.MAX_VALUE}.
     * 
     * @param requested
     *            atomic field updater for a request count
     * @param object
     *            contains the field updated by the updater
     * @param n
     *            the number of requests to add to the requested count
     * @return requested value just prior to successful addition
     */
    public static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> requested, T object, long n) {
        // add n to field but check for overflow
        while (true) {
            long current = requested.get(object);
            long next = current + n;
            // check for overflow
            if (next < 0) {
                next = Long.MAX_VALUE;
            }
            if (requested.compareAndSet(object, current, next)) {
                return current;
            }
        }
    }

    /**
     * Adds {@code n} to {@code requested} and returns the value prior to
     * addition once the addition is successful (uses CAS semantics). If
     * overflows then sets {@code requested} field to {@code Long.MAX_VALUE}.
     * 
     * @param requested
     *            atomic field updater for a request count
     * @param object
     *            contains the field updated by the updater
     * @param n
     *            the number of requests to add to the requested count
     * @return requested value just prior to successful addition
     */
    public static long getAndAddRequest(AtomicLong requested, long n) {
        // add n to field but check for overflow
        while (true) {
            long current = requested.get();
            long next = current + n;
            // check for overflow
            if (next < 0) {
                next = Long.MAX_VALUE;
            }
            if (requested.compareAndSet(current, next)) {
                return current;
            }
        }
    }
}

Related

  1. generateId()
  2. generateRandom()
  3. get(T stat, AtomicIntegerFieldUpdater updater, boolean reset)
  4. getAndAddCap(AtomicLongFieldUpdater updater, T instance, long n)
  5. getAndAddRequest(AtomicLong requested, long n)
  6. getAtomicLongArraySameLengthAsList(List list)
  7. getBench(Map times, int amount)
  8. getDateSafe()
  9. getForeverUniqueID()