List of usage examples for java.util.concurrent.atomic AtomicLong compareAndSet
public final boolean compareAndSet(long expectedValue, long newValue)
From source file:Main.java
public static void main(String[] argv) { AtomicLong nextId = new AtomicLong(); nextId.compareAndSet(122L, 123L); System.out.println(nextId.getAndIncrement()); }
From source file:Main.java
public static long getAndAddRequest(AtomicLong atomiclong, long l) { long l1;//from w ww. j a v a 2s . com do { l1 = atomiclong.get(); } while (!atomiclong.compareAndSet(l1, addCap(l1, l))); return l1; }
From source file:Main.java
static <T> long getAndAddRequest(AtomicLong paramAtomicLong, long paramLong) { long l1;/*from ww w. ja va 2s. c om*/ long l2; do { l1 = paramAtomicLong.get(); l2 = l1 + paramLong; if (l2 < 0L) l2 = 9223372036854775807L; } while (!paramAtomicLong.compareAndSet(l1, l2)); return l1; }
From source file:Main.java
public static long getAndAddRequest(AtomicLong requested, long n) { long current; long next;/*from w w w .j av a 2s .c o m*/ do { current = requested.get(); next = current + n; if (next < 0) { next = Long.MAX_VALUE; } } while (!requested.compareAndSet(current, next)); return current; }
From source file:com.alibaba.rocketmq.example.benchmark.Consumer.java
public static void compareAndSetMax(final AtomicLong target, final long value) { long prev = target.get(); while (value > prev) { boolean updated = target.compareAndSet(prev, value); if (updated) break; prev = target.get();/*ww w . j av a 2 s . c om*/ } }
From source file:Main.java
/** * 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}. * /*from w w w . j ava2s .c om*/ * @param requested * atomic long that should be updated * @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 = addCap(current, n); if (requested.compareAndSet(current, next)) { return current; } } }
From source file:Main.java
/** * Atomically subtracts a value from the requested amount unless it's at Long.MAX_VALUE. * @param requested the requested amount holder * @param n the value to subtract from the requested amount, has to be positive (not verified) * @return the new requested amount/*from w w w . j a va 2s . c om*/ * @throws IllegalStateException if n is greater than the current requested amount, which * indicates a bug in the request accounting logic */ public static long produced(AtomicLong requested, long n) { for (;;) { long current = requested.get(); if (current == Long.MAX_VALUE) { return Long.MAX_VALUE; } long next = current - n; if (next < 0L) { throw new IllegalStateException("More produced than requested: " + next); } if (requested.compareAndSet(current, next)) { return next; } } }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static void atomicSetMax(AtomicLong atomic, long i) { while (true) { long j = atomic.get(); if (j >= i || atomic.compareAndSet(j, i)) break; }//from ww w .jav a 2 s . co m }
From source file:com.alibaba.rocketmq.common.MixAll.java
public static boolean compareAndIncreaseOnly(final AtomicLong target, final long value) { long prev = target.get(); while (value > prev) { boolean updated = target.compareAndSet(prev, value); if (updated) return true; prev = target.get();/* ww w . j a v a 2 s.co m*/ } return false; }
From source file:com.nextdoor.bender.handler.BaseHandler.java
private static void updateOldest(AtomicLong max, long time) { while (true) { long curMax = max.get(); /*//from w w w. ja v a 2 s.c om * With time smaller value is older */ if (curMax <= time) { return; } if (max.compareAndSet(curMax, time)) { return; } } }