本文整理匯總了Java中java.util.concurrent.atomic.AtomicLongFieldUpdater.addAndGet方法的典型用法代碼示例。如果您正苦於以下問題:Java AtomicLongFieldUpdater.addAndGet方法的具體用法?Java AtomicLongFieldUpdater.addAndGet怎麽用?Java AtomicLongFieldUpdater.addAndGet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.atomic.AtomicLongFieldUpdater
的用法示例。
在下文中一共展示了AtomicLongFieldUpdater.addAndGet方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testFieldUpdaters_ClassCastException
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //導入方法依賴的package包/類
/**
* Object arguments for parameters of type T that are not
* instances of the class passed to the newUpdater call will
* result in a ClassCastException being thrown.
*/
public void testFieldUpdaters_ClassCastException() {
// Use raw types to allow passing wrong object type, provoking CCE
final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater();
final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater();
final AtomicReferenceFieldUpdater refUpdater = anIntegerFieldUpdater();
final Object obj = new Object();
for (Object x : new Object[]{ new Object(), null }) {
Runnable[] throwingActions = {
() -> longUpdater.get(x),
() -> intUpdater.get(x),
() -> refUpdater.get(x),
() -> longUpdater.set(x, 17L),
() -> intUpdater.set(x, 17),
() -> refUpdater.set(x, (Integer) 17),
() -> longUpdater.addAndGet(x, 17L),
() -> intUpdater.addAndGet(x, 17),
() -> longUpdater.getAndUpdate(x, y -> y),
() -> intUpdater.getAndUpdate(x, y -> y),
() -> refUpdater.getAndUpdate(x, y -> y),
() -> longUpdater.compareAndSet(x, 17L, 42L),
() -> intUpdater.compareAndSet(x, 17, 42),
() -> refUpdater.compareAndSet(x, (Integer) 17, (Integer) 42),
};
assertThrows(ClassCastException.class, throwingActions);
}
}
示例2: postCompleteDrainDelayError
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //導入方法依賴的package包/類
/**
* Drains the queue either in a pre- or post-complete state, delaying an
* optional error to the end of the drain operation.
*
* @param n the requested amount
* @param actual the consumer of values
* @param queue the queue holding available values
* @param field the field updater holding the requested amount
* @param instance the parent instance of the requested field
* @param isCancelled callback to detect cancellation
* @param error the delayed error
* @return true if the queue was completely drained or the drain process was cancelled
*/
static <T, F> boolean postCompleteDrainDelayError(long n,
Subscriber<? super T> actual,
Queue<T> queue,
AtomicLongFieldUpdater<F> field,
F instance,
BooleanSupplier isCancelled,
@Nullable Throwable error) {
long e = n & COMPLETED_MASK;
for (; ; ) {
while (e != n) {
if (isCancelled.getAsBoolean()) {
return true;
}
T t = queue.poll();
if (t == null) {
if (error == null) {
actual.onComplete();
} else {
actual.onError(error);
}
return true;
}
actual.onNext(t);
e++;
}
if (isCancelled.getAsBoolean()) {
return true;
}
if (queue.isEmpty()) {
if (error == null) {
actual.onComplete();
} else {
actual.onError(error);
}
return true;
}
n = field.get(instance);
if (n == e) {
n = field.addAndGet(instance, -(e & REQUESTED_MASK));
if ((n & REQUESTED_MASK) == 0L) {
return false;
}
e = n & COMPLETED_MASK;
}
}
}