本文整理匯總了Java中java.util.concurrent.atomic.AtomicReferenceArray類的典型用法代碼示例。如果您正苦於以下問題:Java AtomicReferenceArray類的具體用法?Java AtomicReferenceArray怎麽用?Java AtomicReferenceArray使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AtomicReferenceArray類屬於java.util.concurrent.atomic包,在下文中一共展示了AtomicReferenceArray類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: offer
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
public boolean offer(E e) {
if (e == null) {
throw new NullPointerException("Null is not a valid element");
}
AtomicReferenceArray<E> buffer = this.buffer;
int mask = this.mask;
long index = this.producerIndex.get();
int offset = calcElementOffset(index, mask);
if (index >= this.producerLookAhead) {
int step = this.lookAheadStep;
if (lvElement(buffer, calcElementOffset(((long) step) + index, mask)) == null) {
this.producerLookAhead = ((long) step) + index;
} else if (lvElement(buffer, offset) != null) {
return false;
}
}
soProducerIndex(1 + index);
soElement(buffer, offset, e);
return true;
}
示例2: 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;
}
示例3: newResponse
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
@Override
protected NodesGatewayMetaState newResponse(Request request, AtomicReferenceArray responses) {
final List<NodeGatewayMetaState> 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 NodeGatewayMetaState) { // will also filter out null response for unallocated ones
nodesList.add((NodeGatewayMetaState) resp);
} else if (resp instanceof FailedNodeException) {
failures.add((FailedNodeException) resp);
} else {
logger.warn("unknown response type [{}], expected NodeLocalGatewayMetaState or FailedNodeException", resp);
}
}
return new NodesGatewayMetaState(clusterName, nodesList.toArray(new NodeGatewayMetaState[nodesList.size()]),
failures.toArray(new FailedNodeException[failures.size()]));
}
示例4: start
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
public void start() {
if (shardsIts.size() == 0) {
// no shards
try {
listener.onResponse(newResponse(request, new AtomicReferenceArray(0), clusterState));
} catch (Exception e) {
listener.onFailure(e);
}
return;
}
// count the local operations, and perform the non local ones
int shardIndex = -1;
for (final ShardIterator shardIt : shardsIts) {
shardIndex++;
final ShardRouting shard = shardIt.nextOrNull();
if (shard != null) {
performOperation(shardIt, shard, shardIndex);
} else {
// really, no shards active in this group
onOperation(null, shardIt, shardIndex, new NoShardAvailableActionException(shardIt.shardId()));
}
}
}
示例5: newResponse
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
@SuppressWarnings("unchecked")
protected TasksResponse newResponse(TasksRequest request, AtomicReferenceArray responses) {
List<TaskResponse> tasks = new ArrayList<>();
List<FailedNodeException> failedNodeExceptions = new ArrayList<>();
List<TaskOperationFailure> taskOperationFailures = new ArrayList<>();
for (int i = 0; i < responses.length(); i++) {
Object response = responses.get(i);
if (response instanceof FailedNodeException) {
failedNodeExceptions.add((FailedNodeException) response);
} else {
NodeTasksResponse tasksResponse = (NodeTasksResponse) response;
if (tasksResponse.results != null) {
tasks.addAll(tasksResponse.results);
}
if (tasksResponse.exceptions != null) {
taskOperationFailures.addAll(tasksResponse.exceptions);
}
}
}
return newResponse(request, tasks, taskOperationFailures, failedNodeExceptions);
}
示例6: arrayFrom
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
private ArrayList arrayFrom(AtomicReferenceArray atomicArray) {
// cache atomic array length in variable
int len = atomicArray.length();
// create new arraylist
ArrayList newList = new ArrayList<>(atomicArray.length());
// iterate over atomic array and copy to arraylist
for (int i = 0; i < len; i++) {
newList.add(atomicArray.get(i));
}
// return new arraylist
return newList;
}
示例7: arrayFrom
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
private ArrayList<TElement> arrayFrom(AtomicReferenceArray<TElement> atomicArray) {
// cache atomic array length in variable
int len = atomicArray.length();
// create new arraylist
ArrayList<TElement> newList = new ArrayList<>(atomicArray.length());
// iterate over atomic array and copy to arraylist
for (int i = 0; i < len; i++) {
newList.add(atomicArray.get(i));
}
// return new arraylist
return newList;
}
示例8: offer
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
public final boolean offer(T e) {
if (e == null) {
throw new NullPointerException();
}
AtomicReferenceArray<Object> buffer = this.producerBuffer;
long index = lpProducerIndex();
int mask = this.producerMask;
int offset = calcWrappedOffset(index, mask);
if (index < this.producerLookAhead) {
return writeToQueue(buffer, e, index, offset);
}
int lookAheadStep = this.producerLookAheadStep;
if (lvElement(buffer, calcWrappedOffset(((long) lookAheadStep) + index, mask)) == null) {
this.producerLookAhead = (((long) lookAheadStep) + index) - 1;
return writeToQueue(buffer, e, index, offset);
} else if (lvElement(buffer, calcWrappedOffset(1 + index, mask)) != null) {
return writeToQueue(buffer, e, index, offset);
} else {
resize(buffer, index, offset, e, (long) mask);
return true;
}
}
示例9: 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));
}
示例10: removeEntryForTesting
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
@GuardedBy("this")
boolean removeEntryForTesting(E entry) {
int hash = entry.getHash();
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()) {
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;
}
示例11: clear
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
void clear() {
if (count != 0) {
lock();
try {
AtomicReferenceArray<E> table = this.table;
for (int i = 0; i < table.length(); ++i) {
table.set(i, null);
}
maybeClearReferenceQueues();
readCount.set(0);
++modCount;
count = 0; // write-volatile
} finally {
unlock();
}
}
}
示例12: testIndexing
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
/**
* get and set for out of bound indices throw IndexOutOfBoundsException
*/
public void testIndexing() {
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE);
for (int index : new int[] { -1, SIZE }) {
final int j = index;
final Runnable[] tasks = {
() -> aa.getPlain(j),
() -> aa.getOpaque(j),
() -> aa.getAcquire(j),
() -> aa.setPlain(j, null),
() -> aa.setOpaque(j, null),
() -> aa.setRelease(j, null),
() -> aa.compareAndExchange(j, null, null),
() -> aa.compareAndExchangeAcquire(j, null, null),
() -> aa.compareAndExchangeRelease(j, null, null),
() -> aa.weakCompareAndSetPlain(j, null, null),
() -> aa.weakCompareAndSetVolatile(j, null, null),
() -> aa.weakCompareAndSetAcquire(j, null, null),
() -> aa.weakCompareAndSetRelease(j, null, null),
};
assertThrows(IndexOutOfBoundsException.class, tasks);
}
}
示例13: newResponse
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
@Override
protected NodesGatewayStartedShards newResponse(Request request, AtomicReferenceArray responses) {
final List<NodeGatewayStartedShards> 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 NodeGatewayStartedShards) { // will also filter out null response for unallocated ones
nodesList.add((NodeGatewayStartedShards) resp);
} else if (resp instanceof FailedNodeException) {
failures.add((FailedNodeException) resp);
} else {
logger.warn("unknown response type [{}], expected NodeLocalGatewayStartedShards or FailedNodeException", resp);
}
}
return new NodesGatewayStartedShards(clusterName, nodesList.toArray(new NodeGatewayStartedShards[nodesList.size()]),
failures.toArray(new FailedNodeException[failures.size()]));
}
示例14: 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();
}
}
示例15: testRemoveEntry
import java.util.concurrent.atomic.AtomicReferenceArray; //導入依賴的package包/類
public void testRemoveEntry() {
MapMakerInternalMap<Object, Object, ?, ?> map =
makeMap(createMapMaker().concurrencyLevel(1).initialCapacity(1));
Segment<Object, Object, ?, ?> segment = map.segments[0];
AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table;
assertEquals(1, table.length());
Object key = new Object();
Object value = new Object();
int hash = map.hash(key);
InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null);
segment.setValueForTesting(entry, value);
// remove absent
assertFalse(segment.removeTableEntryForTesting(entry));
segment.setTableEntryForTesting(0, entry);
segment.count = 1;
assertTrue(segment.removeTableEntryForTesting(entry));
assertEquals(0, segment.count);
assertNull(table.get(0));
}