Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Atomic versions are missing for some primitive types while being present for some?

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?

like image 1000
Geek Avatar asked Sep 18 '13 17:09

Geek


1 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.

like image 121
Peter Lawrey Avatar answered Sep 28 '22 13:09

Peter Lawrey