
com.intellifylearning.stats.AtomicDouble Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of intellisense Show documentation
Show all versions of intellisense Show documentation
IntelliSense Sensor API for Java
package com.intellifylearning.stats;
import static java.lang.Double.doubleToLongBits;
import java.util.concurrent.atomic.AtomicLong;
/**
* A thread-safe and lock-free double implementation
* based on this stack overflow answer:
* http://stackoverflow.com/questions/5505460/java-is-there-no-atomicfloat-or-atomicdouble
*
*/
class AtomicDouble extends Number {
private static final long serialVersionUID = -2480549991498013056L;
private AtomicLong bits;
AtomicDouble() {
this(0);
}
AtomicDouble(double initialValue) {
bits = new AtomicLong(doubleToLongBits(initialValue));
}
public final boolean compareAndSet(double expect, double update) {
return bits.compareAndSet(doubleToLongBits(expect),
doubleToLongBits(update));
}
public final void set(double newValue) {
bits.set(doubleToLongBits(newValue));
}
public final double get() {
return Double.longBitsToDouble(bits.get());
}
@Override
public float floatValue() {
return (float) get();
}
public final double addAndGet(double newValue) {
return Double.longBitsToDouble(bits.addAndGet(doubleToLongBits(newValue)));
}
public final double getAndSet(double newValue) {
return Double.longBitsToDouble(bits.getAndSet(doubleToLongBits(newValue)));
}
public final boolean weakCompareAndSet(float expect, float update) {
return bits.weakCompareAndSet(doubleToLongBits(expect),
doubleToLongBits(update));
}
@Override
public double doubleValue() {
return floatValue();
}
@Override
public int intValue() {
return (int) get();
}
@Override
public long longValue() {
return (long) get();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy