Java: is there no AtomicFloat or AtomicDouble?

后端 未结 8 1457
误落风尘
误落风尘 2020-12-01 03:12

I have found AtomicInteger, AtomicLong, but where is AtomicFloat (or AtomicDouble)? Maybe there is some trick?

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 03:54

    Are you sure you need it?

    Atomic classes are designed primarily as building blocks for implementing non-blocking data structures and related infrastructure classes. The compareAndSet method is not a general replacement for locking. It applies only when critical updates for an object are confined to a single variable.

    Here is an explanation of the problems that atomic variables were designed to solve.

提交回复
热议问题