當前位置: 首頁>>代碼示例>>Java>>正文


Java AtomicLongFieldUpdater.compareAndSet方法代碼示例

本文整理匯總了Java中java.util.concurrent.atomic.AtomicLongFieldUpdater.compareAndSet方法的典型用法代碼示例。如果您正苦於以下問題:Java AtomicLongFieldUpdater.compareAndSet方法的具體用法?Java AtomicLongFieldUpdater.compareAndSet怎麽用?Java AtomicLongFieldUpdater.compareAndSet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.atomic.AtomicLongFieldUpdater的用法示例。


在下文中一共展示了AtomicLongFieldUpdater.compareAndSet方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testCompareAndSetInMultipleThreads

import java.util.concurrent.atomic.AtomicLongFieldUpdater; //導入方法依賴的package包/類
/**
 * compareAndSet in one thread enables another waiting for value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    x = 1;
    final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
    a = updaterFor("x");

    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3))
                Thread.yield();
        }});

    t.start();
    assertTrue(a.compareAndSet(this, 1, 2));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertEquals(3, a.get(this));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:AtomicLongFieldUpdaterTest.java

示例2: 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;
		}
	}
}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:20,代碼來源:Operators.java

示例3: 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;
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:8,代碼來源:BackpressureUtils.java

示例4: 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);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:36,代碼來源:Atomic8Test.java

示例5: 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;
		}
	}

}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:44,代碼來源:DrainUtils.java

示例6: 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;
		}
	}
}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:46,代碼來源:DrainUtils.java

示例7: 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;
        }
    }

}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:45,代碼來源:DrainUtils.java

示例8: 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;
        }
    }
}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:52,代碼來源:DrainUtils.java

示例9: 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;
		}
	}
}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:24,代碼來源:Operators.java

示例10: 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;
}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:24,代碼來源:Operators.java

示例11: 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;
		}
	}
}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:14,代碼來源:Operators.java

示例12: 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;
        }
    }
}
 
開發者ID:davidmoten,項目名稱:rxjava-jdbc,代碼行數:28,代碼來源:RxUtil.java

示例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;
    }
}
 
開發者ID:akarnokd,項目名稱:RxJavaFlow,代碼行數:26,代碼來源:BackpressureUtils.java

示例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;
        }
    }
}
 
開發者ID:davidmoten,項目名稱:rxjava-extras,代碼行數:30,代碼來源:BackpressureUtils.java


注:本文中的java.util.concurrent.atomic.AtomicLongFieldUpdater.compareAndSet方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。