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. 



 






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"Encountered an improper argument"是在使用MDK调试STM32控制器时出现的错误提示。根据引用和引用的信息,这个错误提示是Keil软件的BUG。当下载完成后,使用ST-LINK进行程序仿真时,有时会出现这个错误提示。对话框关闭后,错误提示可能会再次出现,只能使用任务管理器强制停止才能解决。根据引用的个人实测解决办法,您可以尝试更新MDK版本或者使用Windows任务管理器终止来解决这个问题。此外,如果您的项目路径中有亚洲字符,也可能会引发这个错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [“Error: Encountered an improper argument”的解决方法](https://blog.csdn.net/woshi_ziyu/article/details/80642665)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [Keil v5 Error: Encountered an improper argument 个人实测解决办法](https://download.csdn.net/download/QIANG654001342/16773829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值