Declaring an object as volatile

后端 未结 4 1268
有刺的猬
有刺的猬 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 16:01

    This would only make the object reference volatile. In order to make obj.i and obj.c also volatile you have to make them volatile explicitly

    class C{
       volatile int i = 0;
       volatile char c = 'c';
    }
    

提交回复
热议问题