Java provides AtomicInteger
,AtomicLong
etc which basically compiles to CAS
instructions at hardware level. But why such AtomicXXX classes do not exist for other primitive types like short
and floating point numbers like float
and double
?

- 26,489
- 43
- 149
- 227
3 Answers
You can't CAS on less than a word. AtomicBoolean is implemented using an int
and float
could be implemented using an int
and double
using a long
.
AFAIK, these were added as part of Doug Lea's concurrency library being included and there hadn't been enough of a need to have Atmoic versions of these types before.
IMHO an AtomicDouble could be useful, but I avoid using float
whenever possible due to the lack of precision.

- 525,659
- 79
- 751
- 1,130
-
3There's an `AtomicDouble` in Guava. – Sotirios Delimanolis Sep 18 '13 at 17:46
-
@geek the hardware doesn't support it as a single operation. It could be implement but an AtomicShort would be a AtomicInteger with a cast. – Peter Lawrey Sep 18 '13 at 21:11
-
@PeterLawrey Are you saying that exist in standard java libraries now ?Can you elaborate a little bit on "AFAIK, these were added as part of Doug Lea's concurrency library being included and there hadn't been enough of a need to have Atmoic versions of these types before." – Geek Sep 19 '13 at 04:16
-
Doug Lea's libraries were the basis for the concurrency library in Java 5.0, while classes have been added, there hasn't been an AtomicDouble added. You can use an AtomicInteger to store a byte, short, or char. – Peter Lawrey Sep 19 '13 at 07:43
There are too many primitive types:)
If Java support generics for primitive types, we could have Atomic<short>
etc. for all primitive types.
Without that, we have to declare AtomicXxx for each Xxx primitive type. API designers may find that unappealing.
The same thing happens in Java8. We have a generic Function<A,B>
. But for performance reasons we would like to have primitive versions of Function
, so we have IntFunction
which is kind of Function<int,B>
. But that is only done for int, long, double
. There are no ShortFunction
etc. They got too many "primitivized" types [1] and they are sick of it, so they stopped at these 3 types.
[1] http://download.java.net/jdk8/docs/api/java/util/function/package-summary.html
See also Brian's answer : http://mail.openjdk.java.net/pipermail/lambda-dev/2013-March/008535.html

- 19,446
- 5
- 33
- 61
-
1The "too many primitive types" reason doesn't make much sense. And the "if it was defined for all primitive types, it would have to be generic" thing makes even less sense in this particular case. – Theodoros Chatzigiannakis Sep 18 '13 at 18:04
-
if there is only one primitive type left, e.g. `short`, that does not have a corresponding `AtomicXxx` class, you bet they'll add an `AtomicShort` just to be complete. But there are too many primitive types:) – ZhongYu Sep 18 '13 at 18:09
It's quite easy to cap an AtomicInteger to a value. Let's say you want that to be a short. You can wrap the AtomicInteger into your custom type and use the modulo operation to cap the value. After reaching the max value, the value is recycled in the same manner when it would have hit the Integer.MAX value in the original AtomicInteger implementation.
public class AtomicShort
{
private AtomicInteger root = new AtomicInteger();
public short incrementAndGet()
{
return (short) (root.incrementAndGet() % Short.MAX);
}
}

- 2,196
- 2
- 21
- 18