Refine search
/** * Careful here: if threads are mutating the atomicLongArray while this method is executing, the * final long[] will be a "rolling snapshot" of the state of the bit array. This is usually good * enough, but should be kept in mind. */ public static long[] toPlainArray(AtomicLongArray atomicLongArray) { long[] array = new long[atomicLongArray.length()]; for (int i = 0; i < array.length; ++i) { array[i] = atomicLongArray.get(i); } return array; }
/** * Creates a new {@link UniformReservoir}. * * @param size the number of samples to keep in the sampling reservoir */ public UniformReservoir(int size) { this.values = new AtomicLongArray(size); for (int i = 0; i < values.length(); i++) { values.set(i, 0); } count.set(0); }
data.length() == other.data.length(), "BitArrays must be of equal length (%s != %s)", data.length(), other.data.length()); for (int i = 0; i < data.length(); i++) { long otherLong = other.data.get(i); boolean changedAnyBits = true; do { ourLongOld = data.get(i); ourLongNew = ourLongOld | otherLong; if (ourLongOld == ourLongNew) { break; } while (!data.compareAndSet(i, ourLongOld, ourLongNew));
ParallelDispatcher(Subscriber<? super T>[] subscribers, int prefetch) { this.subscribers = subscribers; this.prefetch = prefetch; this.limit = prefetch - (prefetch >> 2); int m = subscribers.length; this.requests = new AtomicLongArray(m + m + 1); this.requests.lazySet(m + m, m); this.emissions = new long[m]; }
@Override public void update(long value) { final long c = count.incrementAndGet(); if (c <= values.length()) { values.set((int) c - 1, value); } else { final long r = nextLong(c); if (r < values.length()) { values.set((int) r, value); } } }
public void add( long value ) { long cursor = valueCursor.getAndIncrement(); long prevValue = values.getAndSet( (int) (cursor % values.length()), value ); total.addAndGet( value - prevValue ); }
public void reset() { count.set(0); values = new AtomicLongArray(DEFAULT_SIZE); } }
/** * Atomically adds the given value to the element at index {@code i}. * * @param i the index * @param delta the value to add * @return the previous value */ @CanIgnoreReturnValue public final double getAndAdd(int i, double delta) { while (true) { long current = longs.get(i); double currentVal = longBitsToDouble(current); double nextVal = currentVal + delta; long next = doubleToRawLongBits(nextVal); if (longs.compareAndSet(i, current, next)) { return currentVal; } } }
/** * Creates a new {@code AtomicDoubleArray} of the given length, with all elements initially zero. * * @param length the length of the array */ public AtomicDoubleArray(int length) { this.longs = new AtomicLongArray(length); }
boolean get(long bitIndex) { return (data.get((int) (bitIndex >>> 6)) & (1L << bitIndex)) != 0; }
/** * @param curTs Current timestamp. * @param i Index. */ private void clearIfObsolete(long curTs, int i) { long cur = taggedCounters.get(i); byte curTag = getTag(cur); long lastTs = lastHitTimes.get(i); if (isObsolete(curTs, lastTs)) { if (taggedCounters.compareAndSet(i, cur, taggedLongZero(++curTag))) // ABA problem prevention. lastHitTimes.set(i, curTs); // If CAS failed, counter is reset by another thread. } }
/** * Sets the element at position {@code i} to the given value. * * @param i the index * @param newValue the new value */ public final void set(int i, double newValue) { long next = doubleToRawLongBits(newValue); longs.set(i, next); }
private long doLongRead(final int idx) { if (samplerThread.get() != null) { return this.longStorage.get(idx); } synchronized (this.longReadPrepLock[idx]) { if (!isLongDirty(idx)) { // no need to prepare if not dirty return this.longStorage.get(idx); } } // this can take a while so release sync prepareThreadStoreList(); synchronized (this.longReadPrepLock[idx]) { if (!clearLongDirty(idx)) { // no need to prepare if not dirty return this.longStorage.get(idx); } long delta = 0; for (ThreadStorage ts : this.threadStoreList) { delta += ts.longStore.getAndSet(idx, 0); } if (delta != 0) { return this.longStorage.addAndGet(idx, delta); } else { return this.longStorage.get(idx); } } }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) { if (parser.lexer.token() == JSONToken.NULL) { parser.lexer.nextToken(JSONToken.COMMA); return null; } JSONArray array = new JSONArray(); parser.parseArray(array); if (clazz == AtomicIntegerArray.class) { AtomicIntegerArray atomicArray = new AtomicIntegerArray(array.size()); for (int i = 0; i < array.size(); ++i) { atomicArray.set(i, array.getInteger(i)); } return (T) atomicArray; } AtomicLongArray atomicArray = new AtomicLongArray(array.size()); for (int i = 0; i < array.size(); ++i) { atomicArray.set(i, array.getLong(i)); } return (T) atomicArray; }
/** * Returns the length of the array. * * @return the length of the array */ public final int length() { return longs.length(); }