Immutable objects are thread safe, but why?

后端 未结 6 1028
后悔当初
后悔当初 2020-11-29 08:11

Lets say for example, a thread is creating and populating the reference variable of an immutable class by creating its object and another thread kicks in before the first on

6条回答
  •  感情败类
    2020-11-29 09:01

    Two threads will not be creating the same object, so no problem there.

    With regards to 'it may be necessary to ensure...', what they are saying is that if you DON'T make all fields final, you will have to ensure correct behavior yourself.

提交回复
热议问题