本文整理匯總了Java中java.util.concurrent.atomic.AtomicReferenceArray.length方法的典型用法代碼示例。如果您正苦於以下問題:Java AtomicReferenceArray.length方法的具體用法?Java AtomicReferenceArray.length怎麽用?Java AtomicReferenceArray.length使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.atomic.AtomicReferenceArray
的用法示例。
在下文中一共展示了AtomicReferenceArray.length方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: newResponse
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
@Override
protected NodesStoreFilesMetaData newResponse(Request request, AtomicReferenceArray responses) {
final List<NodeStoreFilesMetaData> nodeStoreFilesMetaDatas = new ArrayList<>();
final List<FailedNodeException> failures = new ArrayList<>();
for (int i = 0; i < responses.length(); i++) {
Object resp = responses.get(i);
if (resp instanceof NodeStoreFilesMetaData) { // will also filter out null response for unallocated ones
nodeStoreFilesMetaDatas.add((NodeStoreFilesMetaData) resp);
} else if (resp instanceof FailedNodeException) {
failures.add((FailedNodeException) resp);
} else {
logger.warn("unknown response type [{}], expected NodeStoreFilesMetaData or FailedNodeException", resp);
}
}
return new NodesStoreFilesMetaData(clusterName, nodeStoreFilesMetaDatas.toArray(new NodeStoreFilesMetaData[nodeStoreFilesMetaDatas.size()]),
failures.toArray(new FailedNodeException[failures.size()]));
}
示例2: containsValue
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
/**
* This method is a convenience for testing. Code should call {@link
* MapMakerInternalMap#containsValue} directly.
*/
@VisibleForTesting
boolean containsValue(Object value) {
try {
if (count != 0) { // read-volatile
AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
int length = table.length();
for (int i = 0; i < length; ++i) {
for (ReferenceEntry<K, V> e = table.get(i); e != null; e = e.getNext()) {
V entryValue = getLiveValue(e);
if (entryValue == null) {
continue;
}
if (map.valueEquivalence.equivalent(value, entryValue)) {
return true;
}
}
}
}
return false;
} finally {
postReadCleanup();
}
}
示例3: testSegmentRefresh_duplicate
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
public void testSegmentRefresh_duplicate() throws ExecutionException {
LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()
.concurrencyLevel(1));
Segment<Object, Object> segment = map.segments[0];
Object key = new Object();
int hash = map.hash(key);
AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
int index = hash & (table.length() - 1);
// already loading
DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
DummyValueReference<Object, Object> valueRef = DummyValueReference.create(null);
valueRef.setLoading(true);
entry.setValueReference(valueRef);
table.set(index, entry);
assertNull(segment.refresh(key, hash, identityLoader(), false));
}
示例4: containsValue
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
/**
* This method is a convenience for testing. Code should call {@link
* MapMakerInternalMap#containsValue} directly.
*/
@VisibleForTesting
boolean containsValue(Object value) {
try {
if (count != 0) { // read-volatile
AtomicReferenceArray<E> table = this.table;
int length = table.length();
for (int i = 0; i < length; ++i) {
for (E e = table.get(i); e != null; e = e.getNext()) {
V entryValue = getLiveValue(e);
if (entryValue == null) {
continue;
}
if (map.valueEquivalence().equivalent(value, entryValue)) {
return true;
}
}
}
}
return false;
} finally {
postReadCleanup();
}
}
示例5: newResponse
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
@Override
protected NodesSnapshotStatus newResponse(Request request, AtomicReferenceArray responses) {
final List<NodeSnapshotStatus> nodesList = new ArrayList<>();
final List<FailedNodeException> failures = new ArrayList<>();
for (int i = 0; i < responses.length(); i++) {
Object resp = responses.get(i);
if (resp instanceof NodeSnapshotStatus) { // will also filter out null response for unallocated ones
nodesList.add((NodeSnapshotStatus) resp);
} else if (resp instanceof FailedNodeException) {
failures.add((FailedNodeException) resp);
} else {
logger.warn("unknown response type [{}], expected NodeSnapshotStatus or FailedNodeException", resp);
}
}
return new NodesSnapshotStatus(clusterName, nodesList.toArray(new NodeSnapshotStatus[nodesList.size()]),
failures.toArray(new FailedNodeException[failures.size()]));
}
示例6: newResponse
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
/**
* Map the responses into {@code nodeResponseClass} responses and {@link FailedNodeException}s.
*
* @param request The associated request.
* @param nodesResponses All node-level responses
* @return Never {@code null}.
* @throws NullPointerException if {@code nodesResponses} is {@code null}
* @see #newResponse(BaseNodesRequest, List, List)
*/
protected NodesResponse newResponse(NodesRequest request, AtomicReferenceArray nodesResponses) {
final List<NodeResponse> responses = new ArrayList<>();
final List<FailedNodeException> failures = new ArrayList<>();
final boolean accumulateExceptions = accumulateExceptions();
for (int i = 0; i < nodesResponses.length(); ++i) {
Object response = nodesResponses.get(i);
if (response instanceof FailedNodeException) {
if (accumulateExceptions) {
failures.add((FailedNodeException)response);
} else {
logger.warn("not accumulating exceptions, excluding exception from response", (FailedNodeException)response);
}
} else {
responses.add(nodeResponseClass.cast(response));
}
}
return newResponse(request, responses, failures);
}
示例7: 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);
}
示例8: newResponse
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
@Override
protected ExistsResponse newResponse(ExistsRequest request, AtomicReferenceArray shardsResponses, ClusterState clusterState) {
int successfulShards = 0;
int failedShards = 0;
boolean exists = false;
List<ShardOperationFailedException> shardFailures = null;
// if docs do exist, the last response will have exists = true (since we early terminate the shard requests)
for (int i = shardsResponses.length() - 1; i >= 0 ; i--) {
Object shardResponse = shardsResponses.get(i);
if (shardResponse == null) {
// simply ignore non active shards
} else if (shardResponse instanceof BroadcastShardOperationFailedException) {
failedShards++;
if (shardFailures == null) {
shardFailures = new ArrayList<>();
}
shardFailures.add(new DefaultShardOperationFailedException((BroadcastShardOperationFailedException) shardResponse));
} else {
successfulShards++;
if ((exists = ((ShardExistsResponse) shardResponse).exists())) {
successfulShards = shardsResponses.length() - failedShards;
break;
}
}
}
return new ExistsResponse(exists, shardsResponses.length(), successfulShards, failedShards, shardFailures);
}
示例9: removeEntry
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
@VisibleForTesting
@GuardedBy("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;
ReferenceEntry<K, V> newFirst =
removeValueFromChain(
first,
e,
e.getKey(),
hash,
e.getValueReference().get(),
e.getValueReference(),
cause);
newCount = this.count - 1;
table.set(index, newFirst);
this.count = newCount; // write-volatile
return true;
}
}
return false;
}
示例10: replace
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
V replace(K key, int hash, V newValue) {
lock();
try {
preWriteCleanup();
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)) {
// If the value disappeared, this entry is partially collected,
// and we should pretend like it doesn't exist.
V entryValue = e.getValue();
if (entryValue == null) {
if (isCollected(e)) {
int newCount = this.count - 1;
++modCount;
E newFirst = removeFromChain(first, e);
newCount = this.count - 1;
table.set(index, newFirst);
this.count = newCount; // write-volatile
}
return null;
}
++modCount;
setValue(e, newValue);
return entryValue;
}
}
return null;
} finally {
unlock();
}
}
示例11: clear
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
void clear() {
if (count != 0) { // read-volatile
lock();
try {
long now = map.ticker.read();
preWriteCleanup(now);
AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
for (int i = 0; i < table.length(); ++i) {
for (ReferenceEntry<K, V> e = table.get(i); e != null; e = e.getNext()) {
// Loading references aren't actually in the map yet.
if (e.getValueReference().isActive()) {
K key = e.getKey();
V value = e.getValueReference().get();
RemovalCause cause =
(key == null || value == null) ? RemovalCause.COLLECTED : RemovalCause.EXPLICIT;
enqueueNotification(
key, e.getHash(), value, e.getValueReference().getWeight(), cause);
}
}
}
for (int i = 0; i < table.length(); ++i) {
table.set(i, null);
}
clearReferenceQueues();
writeQueue.clear();
accessQueue.clear();
readCount.set(0);
++modCount;
count = 0; // write-volatile
} finally {
unlock();
postWriteCleanup();
}
}
}
示例12: testComputePartiallyCollectedValue
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
public void testComputePartiallyCollectedValue() throws ExecutionException {
CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
CountingLoader loader = new CountingLoader();
LocalCache<Object, Object> map = makeLocalCache(builder);
Segment<Object, Object> segment = map.segments[0];
AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
assertEquals(0, loader.getCount());
Object key = new Object();
int hash = map.hash(key);
Object value = new Object();
int index = hash & (table.length() - 1);
DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
entry.setValueReference(valueRef);
table.set(index, entry);
segment.count++;
assertSame(value, map.get(key, loader));
assertEquals(0, loader.getCount());
assertEquals(1, segment.count);
valueRef.clear();
assertNotSame(value, map.get(key, loader));
assertEquals(1, loader.getCount());
assertEquals(1, segment.count);
}
示例13: remove
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
boolean remove(Object key, int hash, Object value) {
lock();
try {
preWriteCleanup();
int newCount = this.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()) {
K entryKey = e.getKey();
if (e.getHash() == hash
&& entryKey != null
&& map.keyEquivalence.equivalent(key, entryKey)) {
V entryValue = e.getValue();
boolean explicitRemoval = false;
if (map.valueEquivalence().equivalent(value, entryValue)) {
explicitRemoval = true;
} else if (isCollected(e)) {
// TODO(kak): Remove this branch
} else {
return false;
}
++modCount;
E newFirst = removeFromChain(first, e);
newCount = this.count - 1;
table.set(index, newFirst);
this.count = newCount; // write-volatile
return explicitRemoval;
}
}
return false;
} finally {
unlock();
}
}
示例14: 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();
}
}
示例15: replace
import java.util.concurrent.atomic.AtomicReferenceArray; //導入方法依賴的package包/類
boolean replace(K key, int hash, V oldValue, V newValue) {
lock();
try {
preWriteCleanup();
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)) {
// If the value disappeared, this entry is partially collected,
// and we should pretend like it doesn't exist.
V entryValue = e.getValue();
if (entryValue == null) {
if (isCollected(e)) {
int newCount = this.count - 1;
++modCount;
E 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;
setValue(e, newValue);
return true;
} else {
// Mimic
// "if (map.containsKey(key) && map.get(key).equals(oldValue))..."
return false;
}
}
}
return false;
} finally {
unlock();
}
}