Java Code Examples for java.util.concurrent.atomic.AtomicLongFieldUpdater#addAndGet()

The following examples show how to use java.util.concurrent.atomic.AtomicLongFieldUpdater#addAndGet() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static void main(String[] args) {
    Person person = new Person("zhangsan", 11, 170);
    person.setHobby(new Hobby("打球", "足球,篮球"));
    AtomicIntegerFieldUpdater<Person> atomicIntegerFieldUpdater = AtomicIntegerFieldUpdater.newUpdater(Person.class, "age");
    atomicIntegerFieldUpdater.addAndGet(person, 12);

    AtomicLongFieldUpdater<Person> atomicLongFieldUpdater = AtomicLongFieldUpdater.newUpdater(Person.class, "height");
    atomicLongFieldUpdater.addAndGet(person, 180);

    AtomicReferenceFieldUpdater<Person, Hobby> atomicReferenceFieldUpdater = AtomicReferenceFieldUpdater.newUpdater(Person.class, Hobby.class, "hobby");
    atomicReferenceFieldUpdater.getAndSet(person, new Hobby("打球", "排球,羽毛球"));

}
 
Example 2
/**
 * 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);
    }
}
 
Example 3
Source Project: reactor-core   File: DrainUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
	 * Drains the queue either in a pre- or post-complete state.
	 *
	 * @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
	 * @return true if the queue was completely drained or the drain process was cancelled
	 */
	static <T, F> boolean postCompleteDrain(long n,
			Subscriber<? super T> actual,
			Queue<T> queue,
			AtomicLongFieldUpdater<F> field,
			F instance,
			BooleanSupplier isCancelled) {

// TODO enable fast-path
//        if (n == -1 || n == Long.MAX_VALUE) {
//            for (;;) {
//                if (isDisposed.getAsBoolean()) {
//                    break;
//                }
//
//                T v = queue.poll();
//
//                if (v == null) {
//                    actual.onComplete();
//                    break;
//                }
//
//                actual.onNext(v);
//            }
//
//            return true;
//        }

		long e = n & COMPLETED_MASK;

		for (; ; ) {

			while (e != n) {
				if (isCancelled.getAsBoolean()) {
					return true;
				}

				T t = queue.poll();

				if (t == null) {
					actual.onComplete();
					return true;
				}

				actual.onNext(t);
				e++;
			}

			if (isCancelled.getAsBoolean()) {
				return true;
			}

			if (queue.isEmpty()) {
				actual.onComplete();
				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;
			}
		}

	}
 
Example 4
Source Project: reactor-core   File: DrainUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 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;
        }
    }

}