本文整理汇总了Java中java.util.concurrent.atomic.AtomicLongArray.compareAndSet方法的典型用法代码示例。如果您正苦于以下问题:Java AtomicLongArray.compareAndSet方法的具体用法?Java AtomicLongArray.compareAndSet怎么用?Java AtomicLongArray.compareAndSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.atomic.AtomicLongArray
的用法示例。
在下文中一共展示了AtomicLongArray.compareAndSet方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCompareAndSetInMultipleThreads
import java.util.concurrent.atomic.AtomicLongArray; //导入方法依赖的package包/类
/**
* compareAndSet in one thread enables another waiting for value
* to succeed
*/
public void testCompareAndSetInMultipleThreads() throws InterruptedException {
final AtomicLongArray a = new AtomicLongArray(1);
a.set(0, 1);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
while (!a.compareAndSet(0, 2, 3))
Thread.yield();
}});
t.start();
assertTrue(a.compareAndSet(0, 1, 2));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
assertEquals(3, a.get(0));
}
示例2: set
import java.util.concurrent.atomic.AtomicLongArray; //导入方法依赖的package包/类
public void set(int position) {
int segmentPosition = position >>> log2SegmentSize; /// which segment -- div by num bits per segment
int longPosition = (position >>> 6) & segmentMask; /// which long in the segment -- remainder of div by num bits per segment
int bitPosition = position & 0x3F; /// which bit in the long -- remainder of div by num bits in long (64)
AtomicLongArray segment = getSegment(segmentPosition);
long mask = 1L << bitPosition;
// Thread safety: we need to loop until we win the race to set the long value.
while(true) {
// determine what the new long value will be after we set the appropriate bit.
long currentLongValue = segment.get(longPosition);
long newLongValue = currentLongValue | mask;
// if no other thread has modified the value since we read it, we won the race and we are done.
if(segment.compareAndSet(longPosition, currentLongValue, newLongValue))
break;
}
}
示例3: clear
import java.util.concurrent.atomic.AtomicLongArray; //导入方法依赖的package包/类
public void clear(int position) {
int segmentPosition = position >>> log2SegmentSize; /// which segment -- div by num bits per segment
int longPosition = (position >>> 6) & segmentMask; /// which long in the segment -- remainder of div by num bits per segment
int bitPosition = position & 0x3F; /// which bit in the long -- remainder of div by num bits in long (64)
AtomicLongArray segment = getSegment(segmentPosition);
long mask = ~(1L << bitPosition);
// Thread safety: we need to loop until we win the race to set the long value.
while(true) {
// determine what the new long value will be after we set the appropriate bit.
long currentLongValue = segment.get(longPosition);
long newLongValue = currentLongValue & mask;
// if no other thread has modified the value since we read it, we won the race and we are done.
if(segment.compareAndSet(longPosition, currentLongValue, newLongValue))
break;
}
}
示例4: changeWord
import java.util.concurrent.atomic.AtomicLongArray; //导入方法依赖的package包/类
long changeWord(int bitIndex, @NotNull TLongFunction change) {
if (bitIndex < 0) {
throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
}
AtomicLongArray array = getOrCreateArray(bitIndex);
int wordIndexInArray = wordIndexInArray(bitIndex);
long word;
long newWord;
do {
word = array.get(wordIndexInArray);
newWord = change.execute(word);
}
while (!array.compareAndSet(wordIndexInArray, word, newWord));
return word;
}
示例5: _incDouble
import java.util.concurrent.atomic.AtomicLongArray; //导入方法依赖的package包/类
@Override
protected final void _incDouble(int offset, double delta) {
final AtomicLongArray doubleStorage = this.doubleStorage;
while (true) {
final long longValue = doubleStorage.get(offset);
final long newValue = Double.doubleToLongBits(Double
.longBitsToDouble(longValue) + delta);
if (doubleStorage.compareAndSet(offset, longValue, newValue)) {
return;
}
}
}