本文整理了Java中java.util.concurrent.atomic.AtomicReference.weakCompareAndSet()
方法的一些代码示例,展示了AtomicReference.weakCompareAndSet()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AtomicReference.weakCompareAndSet()
方法的具体详情如下:
包路径:java.util.concurrent.atomic.AtomicReference
类名称:AtomicReference
方法名:weakCompareAndSet
[英]Atomically sets the value to the given updated value if the current value == the expected value.
May fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet.
[中]如果当前值==预期值,则自动将该值设置为给定的更新值。
May fail spuriously and does not provide ordering guarantees,so很少是compareAndSet的合适替代品。
代码示例来源:origin: mokies/ratelimitj
public void dispose() {
storedScript.weakCompareAndSet(
expected,
loadScript()
);
}
}
代码示例来源:origin: net.sf.jabb/jabb-core
/**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
*
* <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
* and does not provide ordering guarantees, so is only rarely an
* appropriate alternative to {@code compareAndSet}.
*
* @param expect the expected value
* @param update the new value
* @return true if successful.
*/
public boolean weakCompareAndSet(BigInteger expect, BigInteger update) {
return valueHolder.weakCompareAndSet(expect, update);
}
代码示例来源:origin: the8472/mldht
boolean setState(Set<TaskState> expected, TaskState newState) {
TaskState current;
do {
current = state.get();
if(!expected.contains(current))
return false;
} while(!state.weakCompareAndSet(current, newState));
return true;
}
代码示例来源:origin: the8472/mldht
boolean setState(Set<CONNECTION_STATE> expected, CONNECTION_STATE newState) {
CONNECTION_STATE current;
do {
current = state.get();
if(!expected.contains(current))
return false;
} while(!state.weakCompareAndSet(current, newState));
if(metaHandler != null)
metaHandler.onStateChange(current, newState);
return true;
}
代码示例来源:origin: stackoverflow.com
private static final AtomicReference<Object> ref = new AtomicReference<>();
// <Object> as we require two distinguished values. :(
// (I guess could use a mark.)
public static Singleton getInstance() {
for (;;) { // May want to play with park to avoid spinning.
Object obj = ref.get();
if (obj instanceof Singleton) {
return (Singleton)obj;
}
if (ref.weakCompareAndSet(null, Thread.currentThread())) {
Singleton instance = null; // To reset on fail.
try {
instance = new Singleton();
} finally {
ref.set(instance);
}
return instance;
}
}
}
代码示例来源:origin: com.github.spullara.java/concurrent6
@Override
public void apply(B value) {
if (!ref.weakCompareAndSet(null, value)) {
promise.set(new Pair((T) ref.get(), value));
}
}
});
代码示例来源:origin: com.github.spullara.java/concurrent6
@Override
public void apply(T value) {
if (!ref.weakCompareAndSet(null, value)) {
promise.set(new Pair(value, (B) ref.get()));
}
}
});
代码示例来源:origin: org.fcrepo/modeshape-jcr
@Override
Window rollup() {
// Swap the queue (which should work, since we should be the only concurrent thread doing this) ...
Queue<DurationActivity> durations = null;
if (this.durations.weakCompareAndSet(duration1, duration2)) {
durations = duration1;
} else {
this.durations.weakCompareAndSet(duration2, duration1);
durations = duration2;
}
// Make a copy to minimize the time spent using the durations ...
List<DurationActivity> records = new ArrayList<DurationActivity>(durations);
durations.clear();
// Now add to the largest durations and compute the statistics ...
int numRecords = records.size();
long[] values = new long[numRecords];
int i = 0;
for (DurationActivity record : records) {
values[i++] = record != null ? record.getDuration(TimeUnit.MILLISECONDS) : 0L;
this.largestDurations.add(record);
while (this.largestDurations.size() > this.retentionSize) {
this.largestDurations.poll(); // remove the smallest duration from the front of the queue
}
}
Statistics stats = statisticsFor(values);
return recordStatisticsForLastSecond(stats);
}
代码示例来源:origin: ModeShape/modeshape
@Override
Window rollup() {
// Swap the queue (which should work, since we should be the only concurrent thread doing this) ...
Queue<DurationActivity> durations = null;
if (this.durations.weakCompareAndSet(duration1, duration2)) {
durations = duration1;
} else {
this.durations.weakCompareAndSet(duration2, duration1);
durations = duration2;
}
// Make a copy to minimize the time spent using the durations ...
List<DurationActivity> records = new ArrayList<DurationActivity>(durations);
durations.clear();
// Now add to the largest durations and compute the statistics ...
int numRecords = records.size();
long[] values = new long[numRecords];
int i = 0;
for (DurationActivity record : records) {
values[i++] = record != null ? record.getDuration(TimeUnit.MILLISECONDS) : 0L;
this.largestDurations.add(record);
while (this.largestDurations.size() > this.retentionSize) {
this.largestDurations.poll(); // remove the smallest duration from the front of the queue
}
}
Statistics stats = statisticsFor(values);
return recordStatisticsForLastSecond(stats);
}
内容来源于网络,如有侵权,请联系作者删除!