当前位置: 首页>>代码示例>>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;未经允许,请勿转载。