Here you can find the source of getAndAddRequest(AtomicLongFieldUpdater
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 |
public static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> requested, T object, long n)
//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; } } } }