Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The API docs for the <a href="http://download.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/package-summary.html" rel="noreferrer"><code>java.util.concurrent</code> package</a> states the following:</p> <blockquote> <p><em>[...] Additionally, classes are provided only for those types that are commonly useful in intended applications. For example, there is no atomic class for representing byte. In those infrequent cases where you would like to do so, you can use an <code>AtomicInteger</code> to hold byte values, and cast appropriately. <strong>You can also hold floats using <code>Float.floatToIntBits</code> and <code>Float.intBitstoFloat</code> conversions, and doubles using <code>Double.doubleToLongBits</code> and <code>Double.longBitsToDouble</code> conversions.</em></strong> </p> </blockquote> <p>I'm not claiming it's a convenient solution, but that seems to be the explanation. I suppose you would probably want to wrap an <code>AtomicInteger</code> and provide access methods for <code>getFloat</code> / <code>setFloat</code> etc.</p> <hr> <p>I actually got around writing one. Here you go:</p> <pre><code>import java.util.concurrent.atomic.AtomicInteger; import static java.lang.Float.*; 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(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload