Declaring an object as volatile

后端 未结 4 1342
有刺的猬
有刺的猬 2021-02-05 15:33

If you declare a member variable as volatile in Java, does this mean that all the object\'s data is stored in volatile memory, or that the reference to the object is stored in v

4条回答
  •  忘掉有多难
    2021-02-05 15:58

    private volatile C obj;
    

    That will make only obj volatile.

    Does it make obj.c and obj.i thread safe or not?

    No. To make them thread-safe, you have to synchronize the access to them.

提交回复
热议问题