當前位置: 首頁>>代碼示例>>Java>>正文


Java AtomicReferenceArray.compareAndSet方法代碼示例

本文整理匯總了Java中java.util.concurrent.atomic.AtomicReferenceArray.compareAndSet方法的典型用法代碼示例。如果您正苦於以下問題:Java AtomicReferenceArray.compareAndSet方法的具體用法?Java AtomicReferenceArray.compareAndSet怎麽用?Java AtomicReferenceArray.compareAndSet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.atomic.AtomicReferenceArray的用法示例。


在下文中一共展示了AtomicReferenceArray.compareAndSet方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testCompareAndSetInMultipleThreads

import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
/**
 * compareAndSet in one thread enables another waiting for value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws InterruptedException {
    final AtomicReferenceArray a = new AtomicReferenceArray(1);
    a.set(0, one);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!a.compareAndSet(0, two, three))
                Thread.yield();
        }});

    t.start();
    assertTrue(a.compareAndSet(0, one, two));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(three, a.get(0));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:AtomicReferenceArrayTest.java

示例2: doRemove

import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
private boolean doRemove(final Item<K, V> item, final Table<K, V> table) {
    int hashCode = item.hashCode;

    final AtomicReferenceArray<Item<K, V>[]> array = table.array;
    final int idx = hashCode & array.length() - 1;

    Item<K, V>[] oldRow;

    for (;;) {
        oldRow = array.get(idx);
        if (oldRow == null) {
            return false;
        }
        if (oldRow == RESIZED) {
            boolean result;
            if (result = doRemove(item, table.resizeView)) {
                sizeUpdater.getAndDecrement(table);
            }
            return result;
        }

        int rowIdx = -1;
        for (int i = 0; i < oldRow.length; i ++) {
            if (item == oldRow[i]) {
                rowIdx = i;
                break;
            }
        }
        if (rowIdx == -1) {
            return false;
        }
        if (array.compareAndSet(idx, oldRow, remove(oldRow, rowIdx))) {
            sizeUpdater.getAndDecrement(table);
            return true;
        }
        // row changed, cycle back again
    }
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:39,代碼來源:SecureHashMap.java

示例3: basicFree

import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
private void basicFree(long addr, int idx,
    AtomicReferenceArray<OffHeapStoredObjectAddressStack> freeLists) {
  OffHeapStoredObjectAddressStack clq = freeLists.get(idx);
  if (clq != null) {
    clq.offer(addr);
  } else {
    clq = createFreeListForEmptySlot(freeLists, idx);
    clq.offer(addr);
    if (!freeLists.compareAndSet(idx, null, clq)) {
      clq = freeLists.get(idx);
      clq.offer(addr);
    }
  }
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:15,代碼來源:FreeListManager.java

示例4: createFreeListForEmptySlot

import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
@Override
protected OffHeapStoredObjectAddressStack createFreeListForEmptySlot(
    AtomicReferenceArray<OffHeapStoredObjectAddressStack> freeLists, int idx) {
  if (this.firstTime) {
    this.firstTime = false;
    OffHeapStoredObjectAddressStack clq = super.createFreeListForEmptySlot(freeLists, idx);
    if (!freeLists.compareAndSet(idx, null, clq)) {
      fail("this should never happen. Indicates a concurrent modification");
    }
  }
  return super.createFreeListForEmptySlot(freeLists, idx);
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:13,代碼來源:FreeListManagerTest.java

示例5: doPut

import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
private V doPut(K key, V value, boolean ifAbsent, Table<K, V> table) {
    final int hashCode = hashOf(key);
    final AtomicReferenceArray<Item<K, V>[]> array = table.array;
    final int idx = hashCode & array.length() - 1;

    OUTER: for (;;) {

        // Fetch the table row.
        Item<K, V>[] oldRow = array.get(idx);
        if (oldRow == RESIZED) {
            // row was transported to the new table so recalculate everything
            final V result = doPut(key, value, ifAbsent, table.resizeView);
            // keep a consistent size view though!
            if (result == NONEXISTENT) sizeUpdater.getAndIncrement(table);
            return result;
        }
        if (oldRow != null) {
            // Find the matching Item in the row.
            Item<K, V> oldItem = null;
            for (Item<K, V> tryItem : oldRow) {
                if (equals(key, tryItem.key)) {
                    oldItem = tryItem;
                    break;
                }
            }
            if (oldItem != null) {
                // entry exists; try to return the old value and try to replace the value if allowed.
                V oldItemValue;
                do {
                    oldItemValue = oldItem.value;
                    if (oldItemValue == NONEXISTENT) {
                        // Key was removed; on the next iteration or two the doornail should be gone.
                        continue OUTER;
                    }
                } while (! ifAbsent && ! valueUpdater.compareAndSet(oldItem, oldItemValue, value));
                return oldItemValue;
            }
            // Row exists but item doesn't.
        }

        // Row doesn't exist, or row exists but item doesn't; try and add a new item to the row.
        final Item<K, V> newItem = new Item<>(key, hashCode, value);
        final Item<K, V>[] newRow = addItem(oldRow, newItem);
        if (! array.compareAndSet(idx, oldRow, newRow)) {
            // Nope, row changed; retry.
            continue;
        }

        // Up the table size.
        final int threshold = table.threshold;
        int newSize = sizeUpdater.incrementAndGet(table);
        // >= 0 is really a sign-bit check
        while (newSize >= 0 && (newSize & 0x7fffffff) > threshold) {
            if (sizeUpdater.compareAndSet(table, newSize, newSize | 0x80000000)) {
                resize(table);
                return nonexistent();
            }
            newSize = sizeUpdater.get(table);
        }
        // Success.
        return nonexistent();
    }
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:64,代碼來源:SecureHashMap.java


注:本文中的java.util.concurrent.atomic.AtomicReferenceArray.compareAndSet方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。