Writing long and double is not atomic in Java?

前端 未结 6 1623
甜味超标
甜味超标 2020-12-02 18:37

Reading and writing of a single variable is atomic (language guarantee!), unless the variable is of type long or double.

I was reading

6条回答
  •  心在旅途
    2020-12-02 19:06

    Read answer by maaartinus @ What operations in Java are considered atomic?
    Read answer by Jon Skeet @ When primitive datatypes not thread-safe in Java?

    As per the JLS you can make read and write operation on double and long to be atomic by declaring it as volatile. But this will not ensure ++ to be atomic. That needs concurrent.atomic package.
    Read this answer from Louis Wasserman.

    Also this blog and comments.

提交回复
热议问题