本文整理了Java中java.util.concurrent.atomic.AtomicReference.compareAndSet()
方法的一些代码示例,展示了AtomicReference.compareAndSet()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AtomicReference.compareAndSet()
方法的具体详情如下:
包路径:java.util.concurrent.atomic.AtomicReference
类名称:AtomicReference
方法名:compareAndSet
[英]Atomically sets the value to the given updated value if the current value == the expected value.
[中]如果当前值==预期值,则自动将该值设置为给定的更新值。
代码示例来源:origin: ReactiveX/RxJava
void doTerminate() {
Runnable r = onTerminate.get();
if (r != null && onTerminate.compareAndSet(r, null)) {
r.run();
}
}
代码示例来源:origin: ReactiveX/RxJava
@Override
public void dispose() {
if (subscribers.get() != TERMINATED) {
@SuppressWarnings("unchecked")
InnerSubscriber[] ps = subscribers.getAndSet(TERMINATED);
if (ps != TERMINATED) {
current.compareAndSet(PublishSubscriber.this, null);
SubscriptionHelper.cancel(upstream);
}
}
}
代码示例来源:origin: stackoverflow.com
AtomicReference<Object> cache = new AtomicReference<Object>();
Object cachedValue = new Object();
cache.set(cachedValue);
//... time passes ...
Object cachedValueToUpdate = cache.get();
//... do some work to transform cachedValueToUpdate into a new version
Object newValue = someFunctionOfOld(cachedValueToUpdate);
boolean success = cache.compareAndSet(cachedValue,cachedValueToUpdate);
代码示例来源:origin: prestodb/presto
public void start()
{
DateTime now = DateTime.now();
executionStartTime.compareAndSet(null, now);
// always update last execution start time
lastExecutionStartTime.set(now);
taskContext.start();
}
代码示例来源:origin: ReactiveX/RxJava
void setFirst(Future<?> f) {
for (;;) {
Future<?> current = first.get();
if (current == CANCELLED) {
f.cancel(runner != Thread.currentThread());
return;
}
if (first.compareAndSet(current, f)) {
return;
}
}
}
代码示例来源:origin: redisson/redisson
@Override
public void dispose() {
if (subscribers.get() != TERMINATED) {
@SuppressWarnings("unchecked")
InnerSubscriber[] ps = subscribers.getAndSet(TERMINATED);
if (ps != TERMINATED) {
current.compareAndSet(PublishSubscriber.this, null);
SubscriptionHelper.cancel(s);
}
}
}
代码示例来源:origin: prestodb/presto
public void start()
{
DateTime now = DateTime.now();
executionStartTime.compareAndSet(null, now);
startNanos.compareAndSet(0, System.nanoTime());
startFullGcCount.compareAndSet(-1, gcMonitor.getMajorGcCount());
startFullGcTimeNanos.compareAndSet(-1, gcMonitor.getMajorGcTime().roundTo(NANOSECONDS));
// always update last execution start time
lastExecutionStartTime.set(now);
}
代码示例来源:origin: ReactiveX/RxJava
boolean add(CompletableDisposable inner) {
for (;;) {
CompletableDisposable[] a = observers.get();
if (a == TERMINATED) {
return false;
}
int n = a.length;
CompletableDisposable[] b = new CompletableDisposable[n + 1];
System.arraycopy(a, 0, b, 0, n);
b[n] = inner;
if (observers.compareAndSet(a, b)) {
return true;
}
}
}
代码示例来源:origin: ReactiveX/RxJava
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void onNext(T t) {
SwitchMapMaybeObserver<R> current = inner.get();
if (current != null) {
current.dispose();
}
MaybeSource<? extends R> ms;
try {
ms = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null MaybeSource");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
upstream.dispose();
inner.getAndSet((SwitchMapMaybeObserver)INNER_DISPOSED);
onError(ex);
return;
}
SwitchMapMaybeObserver<R> observer = new SwitchMapMaybeObserver<R>(this);
for (;;) {
current = inner.get();
if (current == INNER_DISPOSED) {
break;
}
if (inner.compareAndSet(current, observer)) {
ms.subscribe(observer);
break;
}
}
}
代码示例来源:origin: apache/flink
@Override
public void serialize(ComplexPayload record, DataOutputView target) throws IOException {
try {
if (currentOwnerThread.compareAndSet(null, Thread.currentThread())) {
target.write(InstantiationUtil.serializeObject(record));
} else {
throw new IllegalStateException("Concurrent access to type serializer detected!");
}
} finally {
currentOwnerThread.set(null);
}
}
代码示例来源:origin: ReactiveX/RxJava
boolean add(InnerCompletableCache inner) {
for (;;) {
InnerCompletableCache[] a = observers.get();
if (a == TERMINATED) {
return false;
}
int n = a.length;
InnerCompletableCache[] b = new InnerCompletableCache[n + 1];
System.arraycopy(a, 0, b, 0, n);
b[n] = inner;
if (observers.compareAndSet(a, b)) {
return true;
}
}
}
代码示例来源:origin: ReactiveX/RxJava
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void onNext(T t) {
SwitchMapSingleObserver<R> current = inner.get();
if (current != null) {
current.dispose();
}
SingleSource<? extends R> ss;
try {
ss = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null SingleSource");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
upstream.dispose();
inner.getAndSet((SwitchMapSingleObserver)INNER_DISPOSED);
onError(ex);
return;
}
SwitchMapSingleObserver<R> observer = new SwitchMapSingleObserver<R>(this);
for (;;) {
current = inner.get();
if (current == INNER_DISPOSED) {
break;
}
if (inner.compareAndSet(current, observer)) {
ss.subscribe(observer);
break;
}
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Apply {@link #beforeCommit(Supplier) beforeCommit} actions, apply the
* request headers/cookies, and write the request body.
* @param writeAction the action to write the request body (may be {@code null})
* @return a completion publisher
*/
protected Mono<Void> doCommit(@Nullable Supplier<? extends Publisher<Void>> writeAction) {
if (!this.state.compareAndSet(State.NEW, State.COMMITTING)) {
return Mono.empty();
}
this.commitActions.add(() ->
Mono.fromRunnable(() -> {
applyHeaders();
applyCookies();
this.state.set(State.COMMITTED);
}));
if (writeAction != null) {
this.commitActions.add(writeAction);
}
List<? extends Publisher<Void>> actions = this.commitActions.stream()
.map(Supplier::get).collect(Collectors.toList());
return Flux.concat(actions).then();
}
代码示例来源:origin: ReactiveX/RxJava
void setRest(Future<?> f) {
for (;;) {
Future<?> current = rest.get();
if (current == CANCELLED) {
f.cancel(runner != Thread.currentThread());
return;
}
if (rest.compareAndSet(current, f)) {
return;
}
}
}
}
代码示例来源:origin: ReactiveX/RxJava
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void onNext(T t) {
SwitchMapSingleObserver<R> current = inner.get();
if (current != null) {
current.dispose();
}
SingleSource<? extends R> ss;
try {
ss = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null SingleSource");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
upstream.cancel();
inner.getAndSet((SwitchMapSingleObserver)INNER_DISPOSED);
onError(ex);
return;
}
SwitchMapSingleObserver<R> observer = new SwitchMapSingleObserver<R>(this);
for (;;) {
current = inner.get();
if (current == INNER_DISPOSED) {
break;
}
if (inner.compareAndSet(current, observer)) {
ss.subscribe(observer);
break;
}
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Apply {@link #beforeCommit(Supplier) beforeCommit} actions, apply the
* response status and headers/cookies, and write the response body.
* @param writeAction the action to write the response body (may be {@code null})
* @return a completion publisher
*/
protected Mono<Void> doCommit(@Nullable Supplier<? extends Mono<Void>> writeAction) {
if (!this.state.compareAndSet(State.NEW, State.COMMITTING)) {
return Mono.empty();
}
this.commitActions.add(() ->
Mono.fromRunnable(() -> {
applyStatusCode();
applyHeaders();
applyCookies();
this.state.set(State.COMMITTED);
}));
if (writeAction != null) {
this.commitActions.add(writeAction);
}
List<? extends Mono<Void>> actions = this.commitActions.stream()
.map(Supplier::get).collect(Collectors.toList());
return Flux.concat(actions).then();
}
代码示例来源:origin: ReactiveX/RxJava
boolean add(CacheDisposable<T> inner) {
for (;;) {
CacheDisposable<T>[] a = observers.get();
if (a == TERMINATED) {
return false;
}
int n = a.length;
@SuppressWarnings("unchecked")
CacheDisposable<T>[] b = new CacheDisposable[n + 1];
System.arraycopy(a, 0, b, 0, n);
b[n] = inner;
if (observers.compareAndSet(a, b)) {
return true;
}
}
}
代码示例来源:origin: ReactiveX/RxJava
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void onNext(T t) {
SwitchMapMaybeObserver<R> current = inner.get();
if (current != null) {
current.dispose();
}
MaybeSource<? extends R> ms;
try {
ms = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null MaybeSource");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
upstream.cancel();
inner.getAndSet((SwitchMapMaybeObserver)INNER_DISPOSED);
onError(ex);
return;
}
SwitchMapMaybeObserver<R> observer = new SwitchMapMaybeObserver<R>(this);
for (;;) {
current = inner.get();
if (current == INNER_DISPOSED) {
break;
}
if (inner.compareAndSet(current, observer)) {
ms.subscribe(observer);
break;
}
}
}
代码示例来源:origin: ReactiveX/RxJava
/**
* Atomically tries to set the given Disposable on the field if it is null or disposes it if
* the field contains {@link #DISPOSED}.
* @param field the target field
* @param d the disposable to set
* @return true if successful, false otherwise
*/
public static boolean trySet(AtomicReference<Disposable> field, Disposable d) {
if (!field.compareAndSet(null, d)) {
if (field.get() == DISPOSED) {
d.dispose();
}
return false;
}
return true;
}
代码示例来源:origin: ReactiveX/RxJava
boolean add(BehaviorSubscription<T> rs) {
for (;;) {
BehaviorSubscription<T>[] a = subscribers.get();
if (a == TERMINATED) {
return false;
}
int len = a.length;
@SuppressWarnings("unchecked")
BehaviorSubscription<T>[] b = new BehaviorSubscription[len + 1];
System.arraycopy(a, 0, b, 0, len);
b[len] = rs;
if (subscribers.compareAndSet(a, b)) {
return true;
}
}
}
内容来源于网络,如有侵权,请联系作者删除!