Java Code Examples for java.util.concurrent.atomic.AtomicReferenceFieldUpdater#getAndSet()

The following examples show how to use java.util.concurrent.atomic.AtomicReferenceFieldUpdater#getAndSet() . 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
Source File: AtomicIntegerFieldUpdaterDemo.java    From JavaCommon with Apache License 2.0 5 votes vote down vote up
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
Source File: Disposables.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
/**
 * Atomically dispose the {@link Disposable} in the field if not already disposed.
 *
 * @param updater the target field updater
 * @param holder the target instance holding the field
 * @return true if the {@link Disposable} held by the field was properly disposed
 */
static <T> boolean dispose(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder) {
	Disposable current = updater.get(holder);
	Disposable d = DISPOSED;
	if (current != d) {
		current = updater.getAndSet(holder, d);
		if (current != d) {
			if (current != null) {
				current.dispose();
			}
			return true;
		}
	}
	return false;
}
 
Example 3
Source File: OperatorDisposables.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
/**
 * Atomically dispose the {@link Disposable} in the field if not already disposed.
 *
 * @param updater the target field updater
 * @param holder the target instance holding the field
 * @return true if the {@link Disposable} held by the field was properly disposed
 */
public static <T> boolean dispose(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder) {
	Disposable current = updater.get(holder);
	Disposable d = DISPOSED;
	if (current != d) {
		current = updater.getAndSet(holder, d);
		if (current != d) {
			if (current != null) {
				current.dispose();
			}
			return true;
		}
	}
	return false;
}
 
Example 4
Source File: Operators.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
/**
 * Atomically terminates the subscription if it is not already a
 * {@link #cancelledSubscription()}, cancelling the subscription and setting the field
 * to the singleton {@link #cancelledSubscription()}.
 *
 * @param <F> the instance type containing the field
 * @param field the field accessor
 * @param instance the parent instance
 * @return true if terminated, false if the subscription was already terminated
 */
public static <F> boolean terminate(AtomicReferenceFieldUpdater<F, Subscription> field,
		F instance) {
	Subscription a = field.get(instance);
	if (a != CancelledSubscription.INSTANCE) {
		a = field.getAndSet(instance, CancelledSubscription.INSTANCE);
		if (a != null && a != CancelledSubscription.INSTANCE) {
			a.cancel();
			return true;
		}
	}
	return false;
}
 
Example 5
Source File: Exceptions.java    From reactor-core with Apache License 2.0 3 votes vote down vote up
/**
 * Atomic utility to safely mark a volatile throwable reference with a terminal
 * marker.
 *
 * @param field the atomic container
 * @param instance the reference instance
 * @param <T> the instance type
 *
 * @return the previously masked throwable
 */
@Nullable
public static <T> Throwable terminate(AtomicReferenceFieldUpdater<T, Throwable> field,
		T instance) {
	Throwable current = field.get(instance);
	if (current != TERMINATED) {
		current = field.getAndSet(instance, TERMINATED);
	}
	return current;
}