本文整理了Java中java.util.concurrent.atomic.AtomicInteger.compareAndSet()
方法的一些代码示例,展示了AtomicInteger.compareAndSet()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AtomicInteger.compareAndSet()
方法的具体详情如下:
包路径:java.util.concurrent.atomic.AtomicInteger
类名称:AtomicInteger
方法名:compareAndSet
[英]Atomically sets the value to the given updated value if the current value == the expected value.
[中]如果当前值==预期值,则自动将该值设置为给定的更新值。
代码示例来源:origin: ReactiveX/RxJava
public final boolean fastEnter() {
return wip.get() == 0 && wip.compareAndSet(0, 1);
}
代码示例来源:origin: stackoverflow.com
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
/**
* Generate a value suitable for use in {@link #setId(int)}.
* This value will not collide with ID values generated at build time by aapt for R.id.
*
* @return a generated ID value
*/
public static int generateViewId() {
for (;;) {
final int result = sNextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
if (sNextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
}
代码示例来源:origin: apache/incubator-shardingsphere
@Override
public String getDataSource(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) {
AtomicInteger count = COUNT_MAP.containsKey(name) ? COUNT_MAP.get(name) : new AtomicInteger(0);
COUNT_MAP.putIfAbsent(name, count);
count.compareAndSet(slaveDataSourceNames.size(), 0);
return slaveDataSourceNames.get(Math.abs(count.getAndIncrement()) % slaveDataSourceNames.size());
}
}
代码示例来源:origin: ReactiveX/RxJava
@Override
public void request(long n) {
if (n == Long.MAX_VALUE && path.compareAndSet(0, 1)) {
subscriber.onNext("beginningEveryTime");
int i = count.getAndIncrement();
if (n > 0 && req.getAndAdd(n) == 0 && (path.get() == 2 || path.compareAndSet(0, 2)) && !done) {
int i = count.getAndIncrement();
if (i < numFailures) {
代码示例来源:origin: ReactiveX/RxJava
public final boolean fastEnter() {
return wip.get() == 0 && wip.compareAndSet(0, 1);
}
代码示例来源:origin: stackoverflow.com
public class AtomicPseudoRandom extends PseudoRandom {
private AtomicInteger seed;
AtomicPseudoRandom(int seed) {
this.seed = new AtomicInteger(seed);
}
public int nextInt(int n) {
while (true) {
int s = seed.get();
int nextSeed = calculateNext(s);
if (seed.compareAndSet(s, nextSeed)) {
int remainder = s % n;
return remainder > 0 ? remainder : remainder + n;
}
}
}
...
}
代码示例来源:origin: netty/netty
static boolean reserveSpace(AtomicInteger availableSharedCapacity, int space) {
assert space >= 0;
for (;;) {
int available = availableSharedCapacity.get();
if (available < space) {
return false;
}
if (availableSharedCapacity.compareAndSet(available, available - space)) {
return true;
}
}
}
}
代码示例来源:origin: google/guava
return 0;
} else {
existingCounter = countMap.putIfAbsent(element, new AtomicInteger(count));
if (existingCounter == null) {
return 0;
int oldValue = existingCounter.get();
if (oldValue == 0) {
if (count == 0) {
return 0;
} else {
AtomicInteger newCounter = new AtomicInteger(count);
if ((countMap.putIfAbsent(element, newCounter) == null)
|| countMap.replace(element, existingCounter, newCounter)) {
if (existingCounter.compareAndSet(oldValue, count)) {
if (count == 0) {
代码示例来源:origin: redisson/redisson
int getNextIndex() {
int index;
for (; ; ) {
index = this.index.get() + 1;
if (index == poolsize) {
if (this.index.compareAndSet(index - 1, 0)) {
return 0;
}
}
else if(this.index.compareAndSet(index - 1, index)) {
return index;
}
}
}
代码示例来源:origin: google/guava
} else {
return countMap.putIfAbsent(element, new AtomicInteger(newCount)) == null;
int oldValue = existingCounter.get();
if (oldValue == expectedOldCount) {
if (oldValue == 0) {
return true;
} else {
AtomicInteger newCounter = new AtomicInteger(newCount);
return (countMap.putIfAbsent(element, newCounter) == null)
|| countMap.replace(element, existingCounter, newCounter);
if (existingCounter.compareAndSet(oldValue, newCount)) {
if (newCount == 0) {
代码示例来源:origin: redisson/redisson
public int tryAcquire() {
while (true) {
int value = subscribedChannelsAmount.get();
if (value == 0) {
return -1;
}
if (subscribedChannelsAmount.compareAndSet(value, value - 1)) {
return value - 1;
}
}
}
代码示例来源:origin: google/guava
AtomicInteger existingCounter = Maps.safeGet(countMap, element);
if (existingCounter == null) {
existingCounter = countMap.putIfAbsent(element, new AtomicInteger(occurrences));
if (existingCounter == null) {
return 0;
int oldValue = existingCounter.get();
if (oldValue != 0) {
try {
int newValue = IntMath.checkedAdd(oldValue, occurrences);
if (existingCounter.compareAndSet(oldValue, newValue)) {
AtomicInteger newCounter = new AtomicInteger(occurrences);
if ((countMap.putIfAbsent(element, newCounter) == null)
|| countMap.replace(element, existingCounter, newCounter)) {
代码示例来源:origin: redisson/redisson
public int tryAcquire() {
while (true) {
int value = subscribedChannelsAmount.get();
if (value == 0) {
return -1;
}
if (subscribedChannelsAmount.compareAndSet(value, value - 1)) {
return value - 1;
}
}
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testAcquireDifferentLocksOnSameEventLoop() {
Vertx vertx = getVertx();
Context context = vertx.getOrCreateContext();
SharedData sharedData = vertx.sharedData();
AtomicInteger stage = new AtomicInteger();
context.runOnContext(v -> {
sharedData.getLock("foo", onSuccess(foo -> {
assertTrue(stage.compareAndSet(0, 1));
// Create another lock request
sharedData.getLock("foo", onSuccess(foo1 -> {
assertEquals(2, stage.get());
foo1.release();
testComplete();
}));
// Should not be blocked by second request for lock "foo"
sharedData.getLock("bar", onSuccess(bar -> {
assertTrue(stage.compareAndSet(1, 2));
foo.release();
bar.release();
}));
}));
});
await();
}
代码示例来源:origin: Netflix/eureka
private boolean consumeToken(int burstSize) {
while (true) {
int currentLevel = consumedTokens.get();
if (currentLevel >= burstSize) {
return false;
}
if (consumedTokens.compareAndSet(currentLevel, currentLevel + 1)) {
return true;
}
}
}
代码示例来源:origin: apache/flink
private int nextIndex() {
int currIndex, newIndex;
do {
currIndex = next.get();
newIndex = currIndex + 1;
if (newIndex == tempDirectories.length) {
newIndex = 0;
}
}
while (!next.compareAndSet(currIndex, newIndex));
return currIndex;
}
代码示例来源:origin: redisson/redisson
static boolean reserveSpace(AtomicInteger availableSharedCapacity, int space) {
assert space >= 0;
for (;;) {
int available = availableSharedCapacity.get();
if (available < space) {
return false;
}
if (availableSharedCapacity.compareAndSet(available, available - space)) {
return true;
}
}
}
}
代码示例来源:origin: apache/incubator-dubbo
public boolean isAllowable() {
long now = System.currentTimeMillis();
if (now > lastResetTime + interval) {
token.set(rate);
lastResetTime = now;
}
int value = token.get();
boolean flag = false;
while (value > 0 && !flag) {
flag = token.compareAndSet(value, value - 1);
value = token.get();
}
return flag;
}
代码示例来源:origin: apache/incubator-dubbo
public boolean isAllowable() {
long now = System.currentTimeMillis();
if (now > lastResetTime + interval) {
token.set(rate);
lastResetTime = now;
}
int value = token.get();
boolean flag = false;
while (value > 0 && !flag) {
flag = token.compareAndSet(value, value - 1);
value = token.get();
}
return flag;
}
代码示例来源:origin: redisson/redisson
public final boolean fastEnter() {
return wip.get() == 0 && wip.compareAndSet(0, 1);
}
内容来源于网络,如有侵权,请联系作者删除!