本文整理匯總了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;
}
示例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));
}
示例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();
}
}
示例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();
}
}
}
示例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));
}
}
示例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));
}
}
示例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));
}
示例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();
}
}
示例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);
}
示例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();
}
}
示例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));
}
示例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();
}
}
示例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();
}
}
示例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;
}
示例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));
}