本文整理汇总了Java中java.util.concurrent.atomic.AtomicLongFieldUpdater.get方法的典型用法代码示例。如果您正苦于以下问题:Java AtomicLongFieldUpdater.get方法的具体用法?Java AtomicLongFieldUpdater.get怎么用?Java AtomicLongFieldUpdater.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.atomic.AtomicLongFieldUpdater
的用法示例。
在下文中一共展示了AtomicLongFieldUpdater.get方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: producedCancellable
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
static <T> long producedCancellable(AtomicLongFieldUpdater<T> updater, T instance, long n) {
for (; ; ) {
long current = updater.get(instance);
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) {
reportBadRequest(update);
update = 0L;
}
if (updater.compareAndSet(instance, current, update)) {
return update;
}
}
}
示例2: getAndAddRequest
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
public static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> requested, T object, long n) {
long current;
do {
current = requested.get(object);
} while (!requested.compareAndSet(object, current, addCap(current, n)));
return current;
}
示例3: 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);
}
}
示例4: postCompleteRequest
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
/**
* Perform a potential post-completion request accounting.
*
* @param <T> the output value type
* @param <F> the field type holding the requested amount
* @param n the requested amount
* @param actual the consumer of values
* @param queue the queue holding the available values
* @param field the field updater for the requested amount
* @param instance the parent instance for the requested field
* @param isCancelled callback to detect cancellation
* @return true if the state indicates a completion state.
*/
static <T, F> boolean postCompleteRequest(long n,
Subscriber<? super T> actual,
Queue<T> queue,
AtomicLongFieldUpdater<F> field,
F instance,
BooleanSupplier isCancelled) {
for (; ; ) {
long r = field.get(instance);
// extract the current request amount
long r0 = r & REQUESTED_MASK;
// preserve COMPLETED_MASK and calculate new requested amount
long u = (r & COMPLETED_MASK) | Operators.addCap(r0, n);
if (field.compareAndSet(instance, r, u)) {
// (complete, 0) -> (complete, n) transition then replay
if (r == COMPLETED_MASK) {
postCompleteDrain(n | COMPLETED_MASK, actual, queue, field, instance, isCancelled);
return true;
}
// (active, r) -> (active, r + n) transition then continue with requesting from upstream
return false;
}
}
}
示例5: postComplete
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
/**
* Tries draining the queue if the source just completed.
*
* @param <T> the output value type
* @param <F> the field type holding 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
*/
public static <T, F> void postComplete(CoreSubscriber<? super T> actual,
Queue<T> queue,
AtomicLongFieldUpdater<F> field,
F instance,
BooleanSupplier isCancelled) {
if (queue.isEmpty()) {
actual.onComplete();
return;
}
if (postCompleteDrain(field.get(instance), actual, queue, field, instance, isCancelled)) {
return;
}
for (; ; ) {
long r = field.get(instance);
if ((r & COMPLETED_MASK) != 0L) {
return;
}
long u = r | COMPLETED_MASK;
// (active, r) -> (complete, r) transition
if (field.compareAndSet(instance, r, u)) {
// if the requested amount was non-zero, drain the queue
if (r != 0L) {
postCompleteDrain(u, actual, queue, field, instance, isCancelled);
}
return;
}
}
}
示例6: postCompleteRequestDelayError
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
/**
* Perform a potential post-completion request accounting.
*
* @param <T> the output value type
* @param <F> the field type holding the requested amount
* @param n the request amount
* @param actual the consumer of values
* @param queue the queue of available values
* @param field the field updater for the requested amount
* @param instance the parent instance of the requested field
* @param isCancelled callback to detect cancellation
* @param error if not null, the error to signal after the queue has been drained
* @return true if the state indicates a completion state.
*/
public static <T, F> boolean postCompleteRequestDelayError(long n,
Subscriber<? super T> actual,
Queue<T> queue,
AtomicLongFieldUpdater<F> field,
F instance,
BooleanSupplier isCancelled, Throwable error) {
for (; ; ) {
long r = field.get(instance);
// extract the current request amount
long r0 = r & REQUESTED_MASK;
// preserve COMPLETED_MASK and calculate new requested amount
long u = (r & COMPLETED_MASK) | Operators.addCap(r0, n);
if (field.compareAndSet(instance, r, u)) {
// (complete, 0) -> (complete, n) transition then replay
if (r == COMPLETED_MASK) {
postCompleteDrainDelayError(n | COMPLETED_MASK, actual, queue, field, instance, isCancelled, error);
return true;
}
// (active, r) -> (active, r + n) transition then continue with requesting from upstream
return false;
}
}
}
示例7: postCompleteDelayError
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
/**
* Tries draining the queue if the source just completed.
*
* @param <T> the output value type
* @param <F> the field type holding the requested amount
* @param actual the consumer of values
* @param queue the queue of available values
* @param field the field updater for the requested amount
* @param instance the parent instance of the requested field
* @param isCancelled callback to detect cancellation
* @param error if not null, the error to signal after the queue has been drained
*/
public static <T, F> void postCompleteDelayError(CoreSubscriber<? super T> actual,
Queue<T> queue,
AtomicLongFieldUpdater<F> field,
F instance,
BooleanSupplier isCancelled,
@Nullable Throwable error) {
if (queue.isEmpty()) {
if (error == null) {
actual.onComplete();
} else {
actual.onError(error);
}
return;
}
if (postCompleteDrainDelayError(field.get(instance), actual, queue, field, instance, isCancelled, error)) {
return;
}
for (; ; ) {
long r = field.get(instance);
if ((r & COMPLETED_MASK) != 0L) {
return;
}
long u = r | COMPLETED_MASK;
// (active, r) -> (complete, r) transition
if (field.compareAndSet(instance, r, u)) {
// if the requested amount was non-zero, drain the queue
if (r != 0L) {
postCompleteDrainDelayError(u, actual, queue, field, instance, isCancelled, error);
}
return;
}
}
}
示例8: addCap
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
/**
* Concurrent addition bound to Long.MAX_VALUE.
* Any concurrent write will "happen before" this operation.
*
* @param <T> the parent instance type
* @param updater current field updater
* @param instance current instance to update
* @param toAdd delta to add
* @return value before addition or Long.MAX_VALUE
*/
public static <T> long addCap(AtomicLongFieldUpdater<T> updater, T instance, long toAdd) {
long r, u;
for (;;) {
r = updater.get(instance);
if (r == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
u = addCap(r, toAdd);
if (updater.compareAndSet(instance, r, u)) {
return r;
}
}
}
示例9: produced
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
/**
* Concurrent subtraction bound to 0, mostly used to decrement a request tracker by
* the amount produced by the operator. Any concurrent write will "happen before"
* this operation.
*
* @param <T> the parent instance type
* @param updater current field updater
* @param instance current instance to update
* @param toSub delta to subtract
* @return value after subtraction or zero
*/
public static <T> long produced(AtomicLongFieldUpdater<T> updater, T instance, long toSub) {
long r, u;
do {
r = updater.get(instance);
if (r == 0 || r == Long.MAX_VALUE) {
return r;
}
u = subOrZero(r, toSub);
} while (!updater.compareAndSet(instance, r, u));
return u;
}
示例10: addCapCancellable
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
static <T> long addCapCancellable(AtomicLongFieldUpdater<T> updater, T instance,
long n) {
for (; ; ) {
long r = updater.get(instance);
if (r == Long.MIN_VALUE || r == Long.MAX_VALUE) {
return r;
}
long u = addCap(r, n);
if (updater.compareAndSet(instance, r, u)) {
return r;
}
}
}
示例11: getAndAddRequest
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
/**
* 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;
}
}
}
示例12: 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;
}
}
}
示例13: getAndAddRequest
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
/**
* 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
*/
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;
}
}
示例14: getAndAddRequest
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入方法依赖的package包/类
/**
* 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
* @param <T>
* then type of the volatile being updated
* @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;
}
}
}