final vs volatile guaranntee w.rt to safe publication of objects

前端 未结 2 545
挽巷
挽巷 2021-02-04 02:07

From the book Java concurrency in practice :

To publish an object safely, both the reference to the object and the object\'s state must be made visibl

2条回答
  •  爱一瞬间的悲伤
    2021-02-04 02:38

    There is no difference in what the effects of publishing volatile vs final, except that final can only be set once in the constructor, and thus what you read should never change.

    I believe a properly constructed object is indeed what you refer to, an object whose this reference did not escape its constructor and has been published in a safe manner to the thread that it is used in.

提交回复
热议问题