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


Java AtomicReferenceArray.set方法代码示例

本文整理汇总了Java中java.util.concurrent.atomic.AtomicReferenceArray.set方法的典型用法代码示例。如果您正苦于以下问题:Java AtomicReferenceArray.set方法的具体用法?Java AtomicReferenceArray.set怎么用?Java AtomicReferenceArray.set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.atomic.AtomicReferenceArray的用法示例。


在下文中一共展示了AtomicReferenceArray.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: removeEntry

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
@GuardedBy("Segment.this")
boolean removeEntry(ReferenceEntry<K, V> entry, int hash, RemovalCause cause) {
  int newCount = this.count - 1;
  AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
  int index = hash & (table.length() - 1);
  ReferenceEntry<K, V> first = table.get(index);

  for (ReferenceEntry<K, V> e = first; e != null; e = e.getNext()) {
    if (e == entry) {
      ++modCount;
      enqueueNotification(e.getKey(), hash, e.getValueReference().get(), cause);
      ReferenceEntry<K, V> newFirst = removeFromChain(first, e);
      newCount = this.count - 1;
      table.set(index, newFirst);
      this.count = newCount; // write-volatile
      return true;
    }
  }

  return false;
}
 
开发者ID:s-store,项目名称:s-store,代码行数:22,代码来源:MapMakerInternalMap.java

示例2: 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

示例3: reclaimKey

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
/** Removes an entry whose key has been garbage collected. */
@CanIgnoreReturnValue
boolean reclaimKey(E entry, int hash) {
  lock();
  try {
    int newCount = count - 1;
    AtomicReferenceArray<E> table = this.table;
    int index = hash & (table.length() - 1);
    E first = table.get(index);

    for (E e = first; e != null; e = e.getNext()) {
      if (e == entry) {
        ++modCount;
        E newFirst = removeFromChain(first, e);
        newCount = this.count - 1;
        table.set(index, newFirst);
        this.count = newCount; // write-volatile
        return true;
      }
    }

    return false;
  } finally {
    unlock();
  }
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:27,代码来源:MapMakerInternalMap.java

示例4: reclaimValue

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
/**
 * Removes an entry whose value has been garbage collected.
 */
boolean reclaimValue(K key, int hash, ValueReference<K, V> valueReference) {
  lock();
  try {
    int newCount = this.count - 1;
    AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
    int index = hash & (table.length() - 1);
    ReferenceEntry<K, V> first = table.get(index);

    for (ReferenceEntry<K, V> e = first; e != null; e = e.getNext()) {
      K entryKey = e.getKey();
      if (e.getHash() == hash && entryKey != null
          && map.keyEquivalence.equivalent(key, entryKey)) {
        ValueReference<K, V> v = e.getValueReference();
        if (v == valueReference) {
          ++modCount;
          enqueueNotification(key, hash, valueReference.get(), RemovalCause.COLLECTED);
          ReferenceEntry<K, V> newFirst = removeFromChain(first, e);
          newCount = this.count - 1;
          table.set(index, newFirst);
          this.count = newCount; // write-volatile
          return true;
        }
        return false;
      }
    }

    return false;
  } finally {
    unlock();
    if (!isHeldByCurrentThread()) { // don't cleanup inside of put
      postWriteCleanup();
    }
  }
}
 
开发者ID:s-store,项目名称:s-store,代码行数:38,代码来源:MapMakerInternalMap.java

示例5: testCompareAndExchange

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
/**
 * compareAndExchange succeeds in changing value if equal to
 * expected else fails
 */
public void testCompareAndExchange() {
    AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, one);
        assertEquals(one, aa.compareAndExchange(i, one, two));
        assertEquals(two, aa.compareAndExchange(i, two, m4));
        assertEquals(m4, aa.get(i));
        assertEquals(m4, aa.compareAndExchange(i,m5, seven));
        assertEquals(m4, aa.get(i));
        assertEquals(m4, aa.compareAndExchange(i, m4, seven));
        assertEquals(seven, aa.get(i));
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:AtomicReferenceArray9Test.java

示例6: testGetPlainSet

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
/**
 * getPlain returns the last value set
 */
public void testGetPlainSet() {
    AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, one);
        assertEquals(one, aa.getPlain(i));
        aa.set(i, two);
        assertEquals(two, aa.getPlain(i));
        aa.set(i, m3);
        assertEquals(m3, aa.getPlain(i));
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:AtomicReferenceArray9Test.java

示例7: testSegmentReplace

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
public void testSegmentReplace() {
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().concurrencyLevel(1).expireAfterAccess(99999, SECONDS));
  Segment<Object, Object> segment = map.segments[0];
  // TODO(fry): check recency ordering

  Object key = new Object();
  int hash = map.hash(key);
  Object oldValue = new Object();
  Object newValue = new Object();
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> oldValueRef = DummyValueReference.create(oldValue);
  entry.setValueReference(oldValueRef);

  // no entry
  assertNull(segment.replace(key, hash, newValue));
  assertEquals(0, segment.count);

  // same key
  table.set(index, entry);
  segment.count++;
  assertEquals(1, segment.count);
  assertSame(oldValue, segment.get(key, hash));
  assertSame(oldValue, segment.replace(key, hash, newValue));
  assertEquals(1, segment.count);
  assertSame(newValue, segment.get(key, hash));

  // cleared
  entry.setValueReference(oldValueRef);
  assertSame(oldValue, segment.get(key, hash));
  oldValueRef.clear();
  assertNull(segment.replace(key, hash, newValue));
  assertEquals(0, segment.count);
  assertNull(segment.get(key, hash));
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:39,代码来源:LocalCacheTest.java

示例8: reclaimKey

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
/**
 * Removes an entry whose key has been garbage collected.
 */
boolean reclaimKey(ReferenceEntry<K, V> entry, int hash) {
  lock();
  try {
    int newCount = count - 1;
    AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
    int index = hash & (table.length() - 1);
    ReferenceEntry<K, V> first = table.get(index);

    for (ReferenceEntry<K, V> e = first; e != null; e = e.getNext()) {
      if (e == entry) {
        ++modCount;
        ReferenceEntry<K, V> newFirst =
            removeValueFromChain(
                first,
                e,
                e.getKey(),
                hash,
                e.getValueReference().get(),
                e.getValueReference(),
                RemovalCause.COLLECTED);
        newCount = this.count - 1;
        table.set(index, newFirst);
        this.count = newCount; // write-volatile
        return true;
      }
    }

    return false;
  } finally {
    unlock();
    postWriteCleanup();
  }
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:37,代码来源:LocalCache.java

示例9: testEvictEntries

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
public void testEvictEntries() {
  int maxSize = 10;
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().concurrencyLevel(1).maximumSize(maxSize));
  Segment<Object, Object> segment = map.segments[0];

  // manually add elements to avoid eviction
  int originalCount = 1024;
  ReferenceEntry<Object, Object> entry = null;
  LinkedHashMap<Object, Object> originalMap = Maps.newLinkedHashMap();
  for (int i = 0; i < originalCount; i++) {
    Object key = new Object();
    Object value = new Object();
    AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
    int hash = map.hash(key);
    int index = hash & (table.length() - 1);
    ReferenceEntry<Object, Object> first = table.get(index);
    entry = map.newEntry(key, hash, first);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
    segment.recordWrite(entry, 1, map.ticker.read());
    table.set(index, entry);
    originalMap.put(key, value);
  }
  segment.count = originalCount;
  segment.totalWeight = originalCount;
  assertEquals(originalCount, map.size());
  assertEquals(originalMap, map);

  Iterator<Object> it = originalMap.keySet().iterator();
  for (int i = 0; i < originalCount - maxSize; i++) {
    it.next();
    it.remove();
  }
  segment.evictEntries(entry);
  assertEquals(maxSize, map.size());
  assertEquals(originalMap, map);
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:39,代码来源:LocalCacheTest.java

示例10: clearValueForTesting

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
/** Clears a value that has not yet been set, and thus does not require count to be modified. */
@CanIgnoreReturnValue
boolean clearValueForTesting(
    K key,
    int hash,
    WeakValueReference<K, V, ? extends InternalEntry<K, V, ?>> valueReference) {
  lock();
  try {
    AtomicReferenceArray<E> table = this.table;
    int index = hash & (table.length() - 1);
    E first = table.get(index);

    for (E e = first; e != null; e = e.getNext()) {
      K entryKey = e.getKey();
      if (e.getHash() == hash
          && entryKey != null
          && map.keyEquivalence.equivalent(key, entryKey)) {
        WeakValueReference<K, V, E> v = ((WeakValueEntry<K, V, E>) e).getValueReference();
        if (v == valueReference) {
          E newFirst = removeFromChain(first, e);
          table.set(index, newFirst);
          return true;
        }
        return false;
      }
    }

    return false;
  } finally {
    unlock();
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:33,代码来源:MapMakerInternalMap.java

示例11: testRemoveEntry

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
public void testRemoveEntry() {
  LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()
      .concurrencyLevel(1)
      .initialCapacity(1)
      .maximumSize(SMALL_MAX_SIZE)
      .expireAfterWrite(99999, SECONDS)
      .removalListener(countingRemovalListener()));
  Segment<Object, Object> segment = map.segments[0];
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  assertEquals(1, table.length());

  Object key = new Object();
  Object value = new Object();
  int hash = map.hash(key);
  DummyEntry<Object, Object> entry = createDummyEntry(key, hash, value, null);

  // remove absent
  assertFalse(segment.removeEntry(entry, hash, RemovalCause.COLLECTED));

  // remove live
  segment.recordWrite(entry, 1, map.ticker.read());
  table.set(0, entry);
  segment.count = 1;
  assertTrue(segment.removeEntry(entry, hash, RemovalCause.COLLECTED));
  assertNotificationEnqueued(map, key, value, hash);
  assertTrue(map.removalNotificationQueue.isEmpty());
  assertFalse(segment.accessQueue.contains(entry));
  assertFalse(segment.writeQueue.contains(entry));
  assertEquals(0, segment.count);
  assertNull(table.get(0));
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:32,代码来源:LocalCacheTest.java

示例12: put

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
V put(K key, int hash, V value, boolean onlyIfAbsent) {
  lock();
  try {
    preWriteCleanup();

    int newCount = this.count + 1;
    if (newCount > this.threshold) { // ensure capacity
      expand();
      newCount = this.count + 1;
    }

    AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
    int index = hash & (table.length() - 1);
    ReferenceEntry<K, V> first = table.get(index);

    // Look for an existing entry.
    for (ReferenceEntry<K, V> e = first; e != null; e = e.getNext()) {
      K entryKey = e.getKey();
      if (e.getHash() == hash && entryKey != null
          && map.keyEquivalence.equivalent(key, entryKey)) {
        // We found an existing entry.

        ValueReference<K, V> valueReference = e.getValueReference();
        V entryValue = valueReference.get();

        if (entryValue == null) {
          ++modCount;
          setValue(e, value);
          if (!valueReference.isComputingReference()) {
            enqueueNotification(key, hash, entryValue, RemovalCause.COLLECTED);
            newCount = this.count; // count remains unchanged
          } else if (evictEntries()) { // evictEntries after setting new value
            newCount = this.count + 1;
          }
          this.count = newCount; // write-volatile
          return null;
        } else if (onlyIfAbsent) {
          // Mimic
          // "if (!map.containsKey(key)) ...
          // else return map.get(key);
          recordLockedRead(e);
          return entryValue;
        } else {
          // clobber existing entry, count remains unchanged
          ++modCount;
          enqueueNotification(key, hash, entryValue, RemovalCause.REPLACED);
          setValue(e, value);
          return entryValue;
        }
      }
    }

    // Create a new entry.
    ++modCount;
    ReferenceEntry<K, V> newEntry = newEntry(key, hash, first);
    setValue(newEntry, value);
    table.set(index, newEntry);
    if (evictEntries()) { // evictEntries after setting new value
      newCount = this.count + 1;
    }
    this.count = newCount; // write-volatile
    return null;
  } finally {
    unlock();
    postWriteCleanup();
  }
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:68,代码来源:MapMakerInternalMap.java

示例13: replace

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
boolean replace(K key, int hash, V oldValue, V newValue) {
  lock();
  try {
    preWriteCleanup();

    AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
    int index = hash & (table.length() - 1);
    ReferenceEntry<K, V> first = table.get(index);

    for (ReferenceEntry<K, V> e = first; e != null; e = e.getNext()) {
      K entryKey = e.getKey();
      if (e.getHash() == hash && entryKey != null
          && map.keyEquivalence.equivalent(key, entryKey)) {
        // If the value disappeared, this entry is partially collected,
        // and we should pretend like it doesn't exist.
        ValueReference<K, V> valueReference = e.getValueReference();
        V entryValue = valueReference.get();
        if (entryValue == null) {
          if (isCollected(valueReference)) {
            int newCount = this.count - 1;
            ++modCount;
            enqueueNotification(entryKey, hash, entryValue, RemovalCause.COLLECTED);
            ReferenceEntry<K, V> newFirst = removeFromChain(first, e);
            newCount = this.count - 1;
            table.set(index, newFirst);
            this.count = newCount; // write-volatile
          }
          return false;
        }

        if (map.valueEquivalence.equivalent(oldValue, entryValue)) {
          ++modCount;
          enqueueNotification(key, hash, entryValue, RemovalCause.REPLACED);
          setValue(e, newValue);
          return true;
        } else {
          // Mimic
          // "if (map.containsKey(key) && map.get(key).equals(oldValue))..."
          recordLockedRead(e);
          return false;
        }
      }
    }

    return false;
  } finally {
    unlock();
    postWriteCleanup();
  }
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:51,代码来源:MapMakerInternalMap.java

示例14: doConcurrentGet

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
/**
 * Test-helper method that performs {@code nThreads} concurrent calls to {@code cache.get(key)}
 * or {@code cache.getUnchecked(key)}, and returns a List containing each of the results. The
 * result for any given call to {@code cache.get} or {@code cache.getUnchecked} is the value
 * returned, or the exception thrown.
 *
 * <p>As we iterate from {@code 0} to {@code nThreads}, threads with an even index will call
 * {@code getUnchecked}, and threads with an odd index will call {@code get}. If the cache throws
 * exceptions, this difference may be visible in the returned List.
 */
private static <K> List<Object> doConcurrentGet(final LoadingCache<K, ?> cache, final K key,
    int nThreads, final CountDownLatch gettersStartedSignal) throws InterruptedException {

  final AtomicReferenceArray<Object> result = new AtomicReferenceArray<Object>(nThreads);
  final CountDownLatch gettersComplete = new CountDownLatch(nThreads);
  for (int i = 0; i < nThreads; i++) {
    final int index = i;
    Thread thread = new Thread(new Runnable() {
      @Override public void run() {
        gettersStartedSignal.countDown();
        Object value = null;
        try {
          int mod = index % 3;
          if (mod == 0) {
            value = cache.get(key);
          } else if (mod == 1) {
            value = cache.getUnchecked(key);
          } else {
            cache.refresh(key);
            value = cache.get(key);
          }
          result.set(index, value);
        } catch (Throwable t) {
          result.set(index, t);
        }
        gettersComplete.countDown();
      }
    });
    thread.start();
    // we want to wait until each thread is WAITING - one thread waiting inside CacheLoader.load
    // (in startSignal.await()), and the others waiting for that thread's result.
    while (thread.isAlive() && thread.getState() != Thread.State.WAITING) {
      Thread.yield();
    }
  }
  gettersStartedSignal.countDown();
  gettersComplete.await();

  List<Object> resultList = Lists.newArrayListWithExpectedSize(nThreads);
  for (int i = 0; i < nThreads; i++) {
    resultList.add(result.get(i));
  }
  return resultList;
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:55,代码来源:CacheLoadingTest.java

示例15: testSegmentRemoveValue

import java.util.concurrent.atomic.AtomicReferenceArray; //导入方法依赖的package包/类
public void testSegmentRemoveValue() {
  LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1));
  Segment<Object, Object> segment = map.segments[0];

  Object key = new Object();
  int hash = map.hash(key);
  Object oldValue = new Object();
  Object newValue = new Object();
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  int index = hash & (table.length() - 1);

  DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
  DummyValueReference<Object, Object> oldValueRef = DummyValueReference.create(oldValue);
  entry.setValueReference(oldValueRef);

  // no entry
  assertEquals(0, segment.count);
  assertNull(segment.remove(key, hash));
  assertEquals(0, segment.count);

  // same value
  table.set(index, entry);
  segment.count++;
  assertEquals(1, segment.count);
  assertSame(oldValue, segment.get(key, hash));
  assertTrue(segment.remove(key, hash, oldValue));
  assertEquals(0, segment.count);
  assertNull(segment.get(key, hash));

  // different value
  table.set(index, entry);
  segment.count++;
  assertEquals(1, segment.count);
  assertSame(oldValue, segment.get(key, hash));
  assertFalse(segment.remove(key, hash, newValue));
  assertEquals(1, segment.count);
  assertSame(oldValue, segment.get(key, hash));

  // cleared
  assertSame(oldValue, segment.get(key, hash));
  oldValueRef.clear();
  assertFalse(segment.remove(key, hash, oldValue));
  assertEquals(0, segment.count);
  assertNull(segment.get(key, hash));
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:46,代码来源:LocalCacheTest.java


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