本文整理汇总了Java中java.util.concurrent.atomic.AtomicLongArray类的典型用法代码示例。如果您正苦于以下问题:Java AtomicLongArray类的具体用法?Java AtomicLongArray怎么用?Java AtomicLongArray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AtomicLongArray类属于java.util.concurrent.atomic包,在下文中一共展示了AtomicLongArray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: DecayingEstimatedHistogramReservoir
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
@VisibleForTesting
DecayingEstimatedHistogramReservoir(boolean considerZeroes, int bucketCount, Clock clock)
{
if (bucketCount == DEFAULT_BUCKET_COUNT)
{
if (considerZeroes == true)
{
bucketOffsets = DEFAULT_WITH_ZERO_BUCKET_OFFSETS;
}
else
{
bucketOffsets = DEFAULT_WITHOUT_ZERO_BUCKET_OFFSETS;
}
}
else
{
bucketOffsets = EstimatedHistogram.newOffsets(bucketCount, considerZeroes);
}
decayingBuckets = new AtomicLongArray(bucketOffsets.length + 1);
buckets = new AtomicLongArray(bucketOffsets.length + 1);
this.clock = clock;
decayLandmark = clock.getTime();
}
示例2: write
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
SerializeWriter out = serializer.getWriter();
if (object != null) {
AtomicLongArray array = (AtomicLongArray) object;
int len = array.length();
out.append('[');
for (int i = 0; i < len; i++) {
long val = array.get(i);
if (i != 0) {
out.write(',');
}
out.writeLong(val);
}
out.append(']');
} else if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
out.write("[]");
} else {
out.writeNull();
}
}
示例3: write
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.getWriter();
if (object == null) {
if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
out.write("[]");
} else {
out.writeNull();
}
return;
}
AtomicLongArray array = (AtomicLongArray) object;
int len = array.length();
out.append('[');
for (int i = 0; i < len; ++i) {
long val = array.get(i);
if (i != 0) {
out.write(',');
}
out.writeLong(val);
}
out.append(']');
}
示例4: deserialze
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
if (parser.getLexer().token() == JSONToken.NULL) {
parser.getLexer().nextToken(JSONToken.COMMA);
return null;
}
JSONArray array = new JSONArray();
parser.parseArray(array);
AtomicLongArray atomicArray = new AtomicLongArray(array.size());
for (int i = 0; i < array.size(); ++i) {
atomicArray.set(i, array.getLong(i));
}
return (T) atomicArray;
}
示例5: testIndexing
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
/**
* get and set for out of bound indices throw IndexOutOfBoundsException
*/
public void testIndexing() {
AtomicLongArray aa = new AtomicLongArray(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, 1),
() -> aa.setOpaque(j, 1),
() -> aa.setRelease(j, 1),
() -> aa.compareAndExchange(j, 1, 2),
() -> aa.compareAndExchangeAcquire(j, 1, 2),
() -> aa.compareAndExchangeRelease(j, 1, 2),
() -> aa.weakCompareAndSetPlain(j, 1, 2),
() -> aa.weakCompareAndSetVolatile(j, 1, 2),
() -> aa.weakCompareAndSetAcquire(j, 1, 2),
() -> aa.weakCompareAndSetRelease(j, 1, 2),
};
assertThrows(IndexOutOfBoundsException.class, tasks);
}
}
示例6: testCompareAndSetInMultipleThreads
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
/**
* compareAndSet in one thread enables another waiting for value
* to succeed
*/
public void testCompareAndSetInMultipleThreads() throws InterruptedException {
final AtomicLongArray a = new AtomicLongArray(1);
a.set(0, 1);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
while (!a.compareAndSet(0, 2, 3))
Thread.yield();
}});
t.start();
assertTrue(a.compareAndSet(0, 1, 2));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
assertEquals(3, a.get(0));
}
示例7: testGetAndUpdateNPE
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
/**
* All Atomic getAndUpdate methods throw NullPointerException on
* null function argument
*/
public void testGetAndUpdateNPE() {
Runnable[] throwingActions = {
() -> new AtomicLong().getAndUpdate(null),
() -> new AtomicInteger().getAndUpdate(null),
() -> new AtomicReference().getAndUpdate(null),
() -> new AtomicLongArray(1).getAndUpdate(0, null),
() -> new AtomicIntegerArray(1).getAndUpdate(0, null),
() -> new AtomicReferenceArray(1).getAndUpdate(0, null),
() -> aLongFieldUpdater().getAndUpdate(this, null),
() -> anIntFieldUpdater().getAndUpdate(this, null),
() -> anIntegerFieldUpdater().getAndUpdate(this, null),
////() -> aLongFieldUpdater().getAndUpdate(null, Atomic8Test::addLong17),
////() -> anIntFieldUpdater().getAndUpdate(null, Atomic8Test::addInt17),
////() -> anIntegerFieldUpdater().getAndUpdate(null, Atomic8Test::addInteger17),
};
assertThrows(NullPointerException.class, throwingActions);
}
示例8: testCountingInMultipleThreads
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
/**
* Multiple threads using same array of counters successfully
* update a number of times equal to total count
*/
public void testCountingInMultipleThreads() throws InterruptedException {
final AtomicLongArray aa = new AtomicLongArray(SIZE);
long countdown = 10000;
for (int i = 0; i < SIZE; i++)
aa.set(i, countdown);
Counter c1 = new Counter(aa);
Counter c2 = new Counter(aa);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
t1.start();
t2.start();
t1.join();
t2.join();
assertEquals(c1.counts+c2.counts, SIZE * countdown);
}
示例9: set
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
public void set(int position) {
int segmentPosition = position >>> log2SegmentSize; /// which segment -- div by num bits per segment
int longPosition = (position >>> 6) & segmentMask; /// which long in the segment -- remainder of div by num bits per segment
int bitPosition = position & 0x3F; /// which bit in the long -- remainder of div by num bits in long (64)
AtomicLongArray segment = getSegment(segmentPosition);
long mask = 1L << bitPosition;
// Thread safety: we need to loop until we win the race to set the long value.
while(true) {
// determine what the new long value will be after we set the appropriate bit.
long currentLongValue = segment.get(longPosition);
long newLongValue = currentLongValue | mask;
// if no other thread has modified the value since we read it, we won the race and we are done.
if(segment.compareAndSet(longPosition, currentLongValue, newLongValue))
break;
}
}
示例10: clear
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
public void clear(int position) {
int segmentPosition = position >>> log2SegmentSize; /// which segment -- div by num bits per segment
int longPosition = (position >>> 6) & segmentMask; /// which long in the segment -- remainder of div by num bits per segment
int bitPosition = position & 0x3F; /// which bit in the long -- remainder of div by num bits in long (64)
AtomicLongArray segment = getSegment(segmentPosition);
long mask = ~(1L << bitPosition);
// Thread safety: we need to loop until we win the race to set the long value.
while(true) {
// determine what the new long value will be after we set the appropriate bit.
long currentLongValue = segment.get(longPosition);
long newLongValue = currentLongValue & mask;
// if no other thread has modified the value since we read it, we won the race and we are done.
if(segment.compareAndSet(longPosition, currentLongValue, newLongValue))
break;
}
}
示例11: maxSetBit
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
public long maxSetBit() {
ThreadSafeBitSetSegments segments = this.segments.get();
int segmentIdx = segments.numSegments() - 1;
for(;segmentIdx >= 0; segmentIdx--) {
AtomicLongArray segment = segments.getSegment(segmentIdx);
for(int longIdx=segment.length() - 1; longIdx >= 0; longIdx--) {
long l = segment.get(longIdx);
if(l != 0)
return (segmentIdx << log2SegmentSize) + (longIdx * 64) + (63 - Long.numberOfLeadingZeros(l));
}
}
return -1;
}
示例12: getSegment
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
/**
* Get the segment at <code>segmentIndex</code>. If this segment does not yet exist, create it.
*
* @param segmentIndex
* @return
*/
private AtomicLongArray getSegment(int segmentIndex) {
ThreadSafeBitSetSegments visibleSegments = segments.get();
while(visibleSegments.numSegments() <= segmentIndex) {
/// Thread safety: newVisibleSegments contains all of the segments from the currently visible segments, plus extra.
/// all of the segments in the currently visible segments are canonical and will not change.
ThreadSafeBitSetSegments newVisibleSegments = new ThreadSafeBitSetSegments(visibleSegments, segmentIndex + 1, numLongsPerSegment);
/// because we are using a compareAndSet, if this thread "wins the race" and successfully sets this variable, then the segments
/// which are newly defined in newVisibleSegments become canonical.
if(segments.compareAndSet(visibleSegments, newVisibleSegments)) {
visibleSegments = newVisibleSegments;
} else {
/// If we "lose the race" and are growing the ThreadSafeBitSet segments larger,
/// then we will gather the new canonical sets from the update which we missed on the next iteration of this loop.
/// Newly defined segments in newVisibleSegments will be discarded, they do not get to become canonical.
visibleSegments = segments.get();
}
}
return visibleSegments.getSegment(segmentIndex);
}
示例13: growKeyArray
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
/**
* Grow the key array. All of the values in the current array must be re-hashed and added to the new array.
*/
private void growKeyArray() {
AtomicLongArray newKeys = emptyKeyArray(pointersAndOrdinals.length() * 2);
long valuesToAdd[] = new long[size];
int counter = 0;
/// do not iterate over these values in the same order in which they appear in the hashed array.
/// if we do so, we cause large clusters of collisions to appear (because we resolve collisions with linear probing).
for(int i=0;i<pointersAndOrdinals.length();i++) {
long key = pointersAndOrdinals.get(i);
if(key != EMPTY_BUCKET_VALUE) {
valuesToAdd[counter++] = key;
}
}
Arrays.sort(valuesToAdd);
populateNewHashArray(newKeys, valuesToAdd);
/// 70% load factor
sizeBeforeGrow = (newKeys.length() * 7) / 10;
pointersAndOrdinals = newKeys;
}
示例14: changeWord
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
long changeWord(int bitIndex, @NotNull TLongFunction change) {
if (bitIndex < 0) {
throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
}
AtomicLongArray array = getOrCreateArray(bitIndex);
int wordIndexInArray = wordIndexInArray(bitIndex);
long word;
long newWord;
do {
word = array.get(wordIndexInArray);
newWord = change.execute(word);
}
while (!array.compareAndSet(wordIndexInArray, word, newWord));
return word;
}
示例15: equals
import java.util.concurrent.atomic.AtomicLongArray; //导入依赖的package包/类
/**
* Compares this object against the specified object.
* The result is {@code true} if and only if the argument is
* not {@code null} and is a {@code ConcurrentBitSet} object that has
* exactly the same set of bits set to {@code true} as this bit
* set. That is, for every nonnegative {@code int} index {@code k},
* <pre>((ConcurrentBitSet)obj).get(k) == this.get(k)</pre>
* must be true. The current sizes of the two bit sets are not compared.
*
* @param obj the object to compare with
* @return {@code true} if the objects are the same;
* {@code false} otherwise
* @see #size()
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ConcurrentBitSet)) {
return false;
}
if (this == obj) {
return true;
}
ConcurrentBitSet set = (ConcurrentBitSet)obj;
for (int i = 0; i < arrays.length(); i++) {
AtomicLongArray array1 = arrays.get(i);
AtomicLongArray array2 = set.arrays.get(i);
if (array1 == null && array2 == null) continue;
int size = array1 == null ? array2.length() : array1.length();
for (int k=0; k<size;k++) {
long word1 = array1 == null ? 0 : array1.get(k);
long word2 = array2 == null ? 0 : array2.get(k);
if (word1 != word2) return false;
}
}
return true;
}