Example usage for java.util.concurrent.atomic AtomicLongFieldUpdater get

List of usage examples for java.util.concurrent.atomic AtomicLongFieldUpdater get

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicLongFieldUpdater get.

Prototype

public abstract long get(T obj);

Source Link

Document

Returns the current value held in the field of the given object managed by this updater.

Usage

From source file:Main.java

public static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> requested, T object, long n) {
    long current;
    long next;// w w w. j  av a  2s .  c o  m
    do {
        current = requested.get(object);
        next = current + n;
        if (next < 0) {
            next = Long.MAX_VALUE;
        }
    } while (!requested.compareAndSet(object, current, next));
    return current;
}

From source file:Main.java

static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> paramAtomicLongFieldUpdater, T paramT,
        long paramLong) {
    long l1;//from w  w  w. j  a  v  a2s  .  c  o  m
    long l2;
    do {
        l1 = paramAtomicLongFieldUpdater.get(paramT);
        l2 = l1 + paramLong;
        if (l2 < 0L)
            l2 = 9223372036854775807L;
    } while (!paramAtomicLongFieldUpdater.compareAndSet(paramT, l1, l2));
    return l1;
}

From source file:Main.java

/**
 * 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}.
 * // w ww .  j av a 2  s . c  o  m
 * @param <T> the type of the target object on which the field updater operates
 * 
 * @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
 * @deprecated Android has issues with reflection-based atomics
 */
@Deprecated
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 = addCap(current, n);
        if (requested.compareAndSet(object, current, next)) {
            return current;
        }
    }
}