java.util.concurrent.atomic.AtomicReferenceFieldUpdater#getAndSet()源码实例Demo

下面列出了java.util.concurrent.atomic.AtomicReferenceFieldUpdater#getAndSet() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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("打球", "排球,羽毛球"));

}
 
源代码2 项目: reactor-core   文件: Disposables.java
/**
 * 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;
}
 
源代码3 项目: reactor-core   文件: OperatorDisposables.java
/**
 * 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;
}
 
源代码4 项目: reactor-core   文件: Operators.java
/**
 * 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;
}
 
源代码5 项目: reactor-core   文件: Exceptions.java
/**
 * 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;
}