Effectively Immutable Object

后端 未结 3 1815
眼角桃花
眼角桃花 2021-01-31 22:09

I want to make sure that I correctly understand the \'Effectively Immutable Objects\' behavior according to Java Memory Model.

Let\'s say we have a mutable class which

3条回答
  •  再見小時候
    2021-01-31 22:57

    The question is: Does Java Memory Model guarantee that all threads MUST have Outworld.published.get() == 3 ?

    The short answer is no. Because other threads might access Outworld.published before it has been read.

    After the moment when Outworld.published = val; had been performed, under condition that no other modifications done with the val - yes - it always be 3.

    But if any thread performs val.increment then its value might be different for other threads.

提交回复
热议问题