Improper Publish

There is an example in <<JCP>> 3.5 which demonstrates improper publish.

public class Holder{
    private int n;
    public Holder(int n){this.n = n;}
    public void assertSanity(){
        if(n != n){
            throw new AssertionError("This statement is false.");
        }
    }
}

another class publish Holder object.

public Holder holder;
public void initialize(){
    holder = new Holder(42);
}

Holder object was not properly published. Two things could go wrong with the improperly published object. 

1. Another thread could see the stale value for the "holder" field. The stale value could be null or older reference even though a new value has been placed in "holder".

2. Another thread could see a up-to-date value for "holder" reference, but see a stale values for its state.
  My understanding follows.
   The creating an object order is:
         1. load all classes,including super classes and C itself. (only once)
   2. Perform static initialization (only once)
   3. allocate memory space and swipe to zero.
   4. Assigns the parameter variables of called constructor of class C.
   6. Process any explicit or implicit constructor invocation(a call to this() or super() in the constructor);
   7. Initialize the variables in class C.
   8. Perform instance initialization in class C.
   9. Execute called constructor of class C.
  
The action  creating an object is not atomic. When the thread creating an object finishes to execute Step 3, another thread begin to call the object, the object reference is up-to-date, but the state values are default. So the state values are invalid even though the object reference is right. It is also initialization unsafety.

According to <<Think in Java>>, if a filed is final, compiler ensure that it can not be used before being given an initialized value. I understand that the initialization value can not just be default ZERO value, must be value after finishing executing a constructor. Of course, the initialization value could be default ZERO value after finishing construct. <<JCP>> describes that the Java Memory Model offers a special guarantee of initialization safety for a final field. So since an immutable object needs initialization safety, its all field must be final. In another words, that all fields are final ensures that the object initialization is safe. 



 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值