Volatile or synchronized for primitive type?

前端 未结 5 1506
栀梦
栀梦 2021-02-04 06:52

In Java, assignment is atomic if the size of the variable is less than or equal to 32 bits but is not if more than 32 bits.

What (volatile/synchronized) would be more ef

5条回答
  •  隐瞒了意图╮
    2021-02-04 07:16

    According to the oracle documentation, you can use volatile to refer to Double object:

    volatile Double x = y;
    

    "Writes to and reads of references are always atomic, regardless of whether they are implemented as 32-bit or 64-bit values."

提交回复
热议问题