当前位置: 首页>>代码示例>>Java>>正文


Java AtomicLongArray.compareAndSet方法代码示例

本文整理汇总了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));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:AtomicLongArrayTest.java

示例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;
    }
}
 
开发者ID:Netflix,项目名称:hollow,代码行数:21,代码来源:ThreadSafeBitSet.java

示例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;
    }
}
 
开发者ID:Netflix,项目名称:hollow,代码行数:21,代码来源:ThreadSafeBitSet.java

示例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;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:18,代码来源:ConcurrentBitSet.java

示例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;
    }
  }
}
 
开发者ID:gemxd,项目名称:gemfirexd-oss,代码行数:13,代码来源:Atomic60StatisticsImpl.java


注:本文中的java.util.concurrent.atomic.AtomicLongArray.compareAndSet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。