本文整理汇总了Java中java.util.concurrent.atomic.AtomicLong.compareAndSet方法的典型用法代码示例。如果您正苦于以下问题:Java AtomicLong.compareAndSet方法的具体用法?Java AtomicLong.compareAndSet怎么用?Java AtomicLong.compareAndSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.atomic.AtomicLong
的用法示例。
在下文中一共展示了AtomicLong.compareAndSet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addCancel
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
/**
* Atomically adds the positive value n to the requested value in the AtomicLong and
* caps the result at Long.MAX_VALUE and returns the previous value and
* considers Long.MIN_VALUE as a cancel indication (no addition then).
* @param requested the AtomicLong holding the current requested value
* @param n the value to add, must be positive (not verified)
* @return the original value before the add
*/
public static long addCancel(AtomicLong requested, long n) {
for (;;) {
long r = requested.get();
if (r == Long.MIN_VALUE) {
return Long.MIN_VALUE;
}
if (r == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long u = addCap(r, n);
if (requested.compareAndSet(r, u)) {
return r;
}
}
}
示例2: produced
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
/**
* Atomically subtract the given number (positive, not validated) from the target field unless it contains Long.MAX_VALUE.
* @param requested the target field holding the current requested amount
* @param n the produced element count, positive (not validated)
* @return the new amount
*/
public static long produced(AtomicLong requested, long n) {
for (;;) {
long current = requested.get();
if (current == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long update = current - n;
if (update < 0L) {
RxJavaCommonPlugins.onError(new IllegalStateException("More produced than requested: " + update));
update = 0L;
}
if (requested.compareAndSet(current, update)) {
return update;
}
}
}
示例3: producedCancel
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
/**
* Atomically subtract the given number (positive, not validated) from the target field if
* it doesn't contain Long.MIN_VALUE (indicating some cancelled state) or Long.MAX_VALUE (unbounded mode).
* @param requested the target field holding the current requested amount
* @param n the produced element count, positive (not validated)
* @return the new amount
*/
public static long producedCancel(AtomicLong requested, long n) {
for (;;) {
long current = requested.get();
if (current == Long.MIN_VALUE) {
return Long.MIN_VALUE;
}
if (current == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long update = current - n;
if (update < 0L) {
RxJavaCommonPlugins.onError(new IllegalStateException("More produced than requested: " + update));
update = 0L;
}
if (requested.compareAndSet(current, update)) {
return update;
}
}
}
示例4: setBit
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
protected void setBit(AtomicLong atomic, long lMask) {
long lWord;
do {
lWord = atomic.get();
} while (!atomic.compareAndSet(lWord, lWord | lMask));
if ((atomic.get() & lMask) == 0L) {
throw new InternalError();
}
}
示例5: setIfGreater
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
private boolean setIfGreater(AtomicLong al, long setTo) {
while (true) {
long current = al.get();
if (setTo > current) {
if (al.compareAndSet(current, setTo)) {
return true;
}
} else {
return false;
}
}
}
示例6: getAndAddRequest
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
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;
}
}
}
示例7: compareAndSetMax
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
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();
}
}
示例8: compareAndIncreaseOnly
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
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();
}
return false;
}
示例9: clearBit
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
protected void clearBit(AtomicLong atomic, long lMask) {
long lWord;
do {
lWord = atomic.get();
} while (!atomic.compareAndSet(lWord, lWord & ~lMask));
if ((atomic.get() & lMask) != 0L) {
throw new InternalError();
}
}
示例10: add
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
/**
* Atomically adds the positive value n to the requested value in the AtomicLong and
* caps the result at Long.MAX_VALUE and returns the previous value.
* @param requested the AtomicLong holding the current requested value
* @param n the value to add, must be positive (not verified)
* @return the original value before the add
*/
public static long add(AtomicLong requested, long n) {
for (;;) {
long r = requested.get();
if (r == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long u = addCap(r, n);
if (requested.compareAndSet(r, u)) {
return r;
}
}
}
示例11: postCompleteRequest
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
/**
* Accumulates requests (not validated) and handles the completed mode draining of the queue based on the requests.
*
* <p>
* Post-completion backpressure handles the case when a source produces values based on
* requests when it is active but more values are available even after its completion.
* In this case, the onComplete() can't just emit the contents of the queue but has to
* coordinate with the requested amounts. This requires two distinct modes: active and
* completed. In active mode, requests flow through and the queue is not accessed but
* in completed mode, requests no-longer reach the upstream but help in draining the queue.
*
* @param <T> the value type emitted
* @param n the request amount, positive (not validated)
* @param actual the target Subscriber to send events to
* @param queue the queue to drain if in the post-complete state
* @param state holds the request amount and the post-completed flag
* @param isCancelled a supplier that returns true if the drain has been cancelled
* @return true if the state indicates a completion state.
*/
public static <T> boolean postCompleteRequest(long n,
Subscriber<? super T> actual,
Queue<T> queue,
AtomicLong state,
BooleanSupplier isCancelled) {
for (; ; ) {
long r = state.get();
// extract the current request amount
long r0 = r & REQUESTED_MASK;
// preserve COMPLETED_MASK and calculate new requested amount
long u = (r & COMPLETED_MASK) | BackpressureHelper.addCap(r0, n);
if (state.compareAndSet(r, u)) {
// (complete, 0) -> (complete, n) transition then replay
if (r == COMPLETED_MASK) {
postCompleteDrain(n | COMPLETED_MASK, actual, queue, state, isCancelled);
return true;
}
// (active, r) -> (active, r + n) transition then continue with requesting from upstream
return false;
}
}
}
示例12: decrementRequested
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
private void decrementRequested() {
AtomicLong localRequested = this.requested;
long r;
do {
r = localRequested.get();
if (r == Long.MAX_VALUE) {
return;
}
} while (!localRequested.compareAndSet(r, r - 1));
}
示例13: IRandom
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
public IRandom() {
AtomicLong seedUniquifier = new AtomicLong(8682522807148012L);
long next = 0;
for (;;) {
long current = seedUniquifier.get();
next = current * 181783497276652981L;
if (seedUniquifier.compareAndSet(current, next)){
this.seed = new AtomicLong(next ^ System.nanoTime());
return;
}
}
}
示例14: next
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
private int next() {
long oldseed, nextseed;
AtomicLong seed = this.seed;
do {
oldseed = seed.get();
nextseed = (oldseed * multiplier + addend) & mask;
} while (!seed.compareAndSet(oldseed, nextseed));
return (int) (nextseed >>> (48 - bits));
}
示例15: setNewLargestValue
import java.util.concurrent.atomic.AtomicLong; //导入方法依赖的package包/类
/**
*
* @param value
* @param newValue
*/
private static boolean setNewLargestValue(AtomicLong value, long newValue) {
boolean done = false;
while (!done) {
long oldValue = value.get();
if (oldValue < newValue) {
return value.compareAndSet(oldValue, newValue);
} else {
done = true;
}
}
return false;
}