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

DatecurrentDate()
current Date
return new Date(currentTimeMillis());
LonggenerateId()
Creates a unique id.
return id.incrementAndGet();
StringgenerateId()
generate Id
return counter.getAndIncrement() + "-" + System.currentTimeMillis();
StringgenerateRandom()
Generates a random String.
long next = ATOMIC_COUNT.incrementAndGet();
StringBuffer random = new StringBuffer();
random.append(new Date().getTime()).append('-').append(next);
return random.toString();
intget(T stat, AtomicIntegerFieldUpdater updater, boolean reset)
get
if (reset) {
    return updater.getAndSet(stat, 0);
} else {
    return updater.get(stat);
longgetAndAddCap(AtomicLongFieldUpdater updater, T instance, long n)
Atomically adds the value to the atomic variable, capping the sum at Long.MAX_VALUE and returning the original 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;
...
longgetAndAddRequest(AtomicLong requested, long n)
Adds n (not validated) to requested and returns the value prior to addition once the addition is successful (uses CAS semantics).
while (true) {
    long current = requested.get();
    long next = addCap(current, n);
    if (requested.compareAndSet(current, next)) {
        return current;
longgetAndAddRequest(AtomicLongFieldUpdater requested, T object, long n)
Adds n to requested field and returns the value prior to addition once the addition is successful (uses CAS semantics).
while (true) {
    long current = requested.get(object);
    long next = current + n;
    if (next < 0) {
        next = Long.MAX_VALUE;
    if (requested.compareAndSet(object, current, next)) {
        return current;
...
AtomicLongArraygetAtomicLongArraySameLengthAsList(List list)
get Atomic Long Array Same Length As List
if (list == null) {
    return null;
return new AtomicLongArray(list.size());
StringgetBench(Map times, int amount)
get Bench
long fullScanElapsed = times.get("FullScan").get();
StringBuilder result = new StringBuilder("<b>MicroBench</b> FullScan(ms):"
        + (fullScanElapsed / amount) / 1000 + "  <br><b> Breakdown(%):</b><br>");
for (String key : times.keySet()) {
    long totalNanos = times.get(key).get();
    result.append(key).append(":")
            .append(Math.round((((double) totalNanos / (double) fullScanElapsed) * 100.0))).append("<br>");
return result.toString();