Does immutability guarantee thread safety?

前端 未结 2 882
南旧
南旧 2021-01-24 17:50

Well, consider the immutable class Immutable as given below:

public final class Immutable
{
    final int x;
    final int y;
    public Immutable(i         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-24 18:30

    So, Thread B could see the write to objas occurring before the writes to the fields of the Immutable. Hence Thread B could thus see a partially constructed Immutable that may well be in an invalid state and whose state may unexpectedly change later.

    In Java 1.4, this was true. In Java 5.0 and above, final fields are thread safe after construction.

提交回复
热议问题