Java Utililty Methods AtomicLong

List of utility methods to do AtomicLong

Description

The list of methods to do AtomicLong are organized into topic(s).

Method

longadd(AtomicLong requested, long n)
Atomically adds the positive value n to the requested value in the AtomicLong and caps the result at Long.MAX_VALUE and returns the previous value.
for (;;) {
    long r = requested.get();
    if (r == Long.MAX_VALUE) {
        return Long.MAX_VALUE;
    long u = addCap(r, n);
    if (requested.compareAndSet(r, u)) {
        return r;
...
longadd(AtomicLongFieldUpdater updater, T instance, long n)
Atomically adds the positive value n to the value in the instance through the field updater and caps the result at Long.MAX_VALUE and returns the previous value.
for (;;) {
    long r = updater.get(instance);
    if (r == Long.MAX_VALUE) {
        return Long.MAX_VALUE;
    long u = addCap(r, n);
    if (updater.compareAndSet(instance, r, u)) {
        return r;
...
longaddAndGet(AtomicLong current, long toAdd)
Concurrent addition bound to Long.MAX_VALUE.
long u, r;
do {
    r = current.get();
    if (r == Long.MAX_VALUE) {
        return Long.MAX_VALUE;
    u = addCap(r, toAdd);
} while (!current.compareAndSet(r, u));
...
voidaddstat(Map stat, String key)
addstat
AtomicLong count = stat.get(key);
if (null == count) {
    count = new AtomicLong();
    stat.put(key, count);
count.incrementAndGet();
doubleaverage(Stream stream)
Computes the average of Doubles of a stream.
if (stream.isParallel()) {
    throw new UnsupportedOperationException("Parallel stream is not supported");
AtomicLong al = new AtomicLong();
double sum = stream.reduce(0.0, (x, y) -> {
    al.incrementAndGet();
    return x + y;
});
...
booleancompareAndSetIfGreater(final AtomicLong dest, final long tryValue)
If dest only ever monotonically increases, this function is guaranteed to return with dest having a value of at least tryValue and preserves monotonicity.
long destValue;
do {
    destValue = dest.get();
    if (tryValue <= destValue) {
        return false;
} while (!dest.compareAndSet(destValue, tryValue));
return true;
...
AtomicLongcreateAtomicId()
create Atomic Id
int baseId = new Random(System.currentTimeMillis()).nextInt(1000000) + 20000;
return new AtomicLong((long) baseId);
LongcreateId(final Long baseId)
create Id
final long delta = ((baseId != null ? baseId.longValue() : 0l) - ID_SEQUENCE.get());
long newId = (delta > 0 ? ID_SEQUENCE.addAndGet(delta) : createId());
return Long.valueOf(newId);
longcreateObjectID()
create Object ID
return iCounter.addAndGet(1);
StringcreateUniqueFileName(String out)
create Unique File Name
return out + System.currentTimeMillis() + "-" + incrementor.getAndIncrement();