當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。