Skip to content

Instantly share code, notes, and snippets.

@adohe-zz
Created May 12, 2014 15:44
Show Gist options
  • Select an option

  • Save adohe-zz/7a650fa62df38949c8c9 to your computer and use it in GitHub Desktop.

Select an option

Save adohe-zz/7a650fa62df38949c8c9 to your computer and use it in GitHub Desktop.
add an AtomicFloat implementation
package com.ado.java.odata.util;
import java.util.concurrent.atomic.AtomicInteger;
import static java.lang.Float.*;
/**
* Created with IntelliJ IDEA.
* User: nankonami
* Date: 14-5-12
* Time: 下午11:39
* To change this template use File | Settings | File Templates.
*/
public class AtomicFloat extends Number {
private AtomicInteger bits;
public AtomicFloat() {
this(0f);
}
public AtomicFloat(float initialValue) {
bits = new AtomicInteger(floatToIntBits(initialValue));
}
public final boolean compareAndSet(float expect, float update) {
return bits.compareAndSet(floatToIntBits(expect),
floatToIntBits(update));
}
public final void set(float newValue) {
bits.set(floatToIntBits(newValue));
}
public final float get() {
return intBitsToFloat(bits.get());
}
public float floatValue() {
return get();
}
public final float getAndSet(float newValue) {
return intBitsToFloat(bits.getAndSet(floatToIntBits(newValue)));
}
public final boolean weakCompareAndSet(float expect, float update) {
return bits.weakCompareAndSet(floatToIntBits(expect),
floatToIntBits(update));
}
public double doubleValue() {
return (double) floatValue();
}
public int intValue() {
return (int) get();
}
public long longValue() {
return (long) get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment