分类 云南报名 通知.录取 成绩查询 考试动态 教务管理

Java设计模式:Java中的模式--单态2

五,证明上边的假想是可能发生的,字节码是用来分析问题的最好的工具,可以利用它来分析下边一段程序:(为了分析方便,所以渐少了内容)

  字节码的使用方法见这里,利用字节码分析问题

  class Singleton

  {

  private static Singleton instance;

  private boolean inUse;

  private int val;

  private Singleton()

  {

  inUse = true;

  val = 5;

  }

  public static Singleton getInstance()

  {

  if (instance == null)

  instance = new Singleton();

  return instance;

  }

  }

  得到的字节码

  ;asm code generated for getInstance

  054D20B0   mov         eax,[049388C8]      ;load instance ref

  054D20B5   test        eax,eax             ;test for null

  054D20B7   jne         054D20D7

  054D20B9   mov         eax,14C0988h

  054D20BE   call        503EF8F0            ;allocate memory

  054D20C3   mov         [049388C8],eax      ;store pointer in

  ;instance ref. instance

  ;non-null and ctor

  ;has not run

  054D20C8   mov         ecx,dword ptr [eax]

  054D20CA   mov         dword ptr [ecx],1   ;inline ctor - inUse=true;

  054D20D0   mov         dword ptr [ecx+4],5 ;inline ctor - val=5;

  054D20D7   mov         ebx,dword ptr ds:[49388C8h]

  054D20DD   jmp         054D20B0

  上边的字节码证明,猜想是有可能实现的

六:好了,上边证明Double-checked locking可能出现取出错误数据的情况,那么我们还是可以解决的

  public static Singleton getInstance()

  {

  if (instance == null)

  {

  synchronized(Singleton.class) {      //1

  Singleton inst = instance;         //2

  if (inst == null)

  {

  synchronized(Singleton.class) {  //3

  inst = new Singleton();        //4

  }

  instance = inst;                 //5

  }

  }

  }

  return instance;

  }

  利用Double-checked locking 两次同步,中间变量,解决上边的问题。

  (下边这段话我只能简单的理解,翻译过来不好,所以保留原文,list 7是上边的代码,list 8是下边的

  The code in Listing 7 doesn't work because of the current definition of the memory model.

  The Java Language Specification (JLS) demands that code within a synchronized block

  not be moved out of a synchronized block. However, it does not say that

  code not in a synchronized block cannot be moved into a synchronized block.

  A JIT compiler would see an optimization opportunity here.

  This optimization would remove the code at

  //4 and the code at //5, combine it and generate the code shown in Listing 8:)

  -------------------------------------------------

  list 8

  public static Singleton getInstance()

  {

  if (instance == null)

  {

  synchronized(Singleton.class) {      //1

  Singleton inst = instance;         //2

  if (inst == null)

  {

  synchronized(Singleton.class) {  //3

  //inst = new Singleton();      //4

  instance = new Singleton();

  }

  //instance = inst;               //5

  }

  }

  }

  return instance;

  }

 


 


   >>学课在线网课试听.报名        >>学课在线智能题库.模拟做题       >>直播课程       >>录播课程

课程名称
课程免费试听
课程名称
课程免费试听
课程名称
课程免费试听
初级会计师

一级建造师

执业药师

中级会计师

二级建造师

护士资格证

注册会计师

一级造价工程师

健康管理师

经济师考试

监理工程师

考研辅导课

银行从业

安全工程师

自考课程

基金从业

一级消防工程师

心理咨询师

企业人力资源管理师

法律职业资格考试

公共营养师

社会工作者

消防设施操作员

保育员

教师资格证

成人高考

育婴师

公务员培训

文职培训

英语四六级

计算机软考
养老护理员
税务师培训

房产经纪人
咨询工程师

其它课程>>



重要提醒!!内容中联系方式并非本站联系方式,报名咨询的学员请与下面最新联系方式联系我们咨询报名-以免损失!
>>长期招聘兼职招生代理人员,项目合作,团报优惠咨询,有意者请联系我们 >>咨询:13312524700(可加微信)。

昆明学历中心:◆咨询电话:0871-65385921、13312528471 赖老师、钱老师(微信报名:17787865775)点击这里给我发消息


云南地州中心:◆咨询电话:0871-65385921、17787865775 冯老师、 王老师(微信报名:17787865775)点击这里给我发消息


总部报名地址: ◆昆明市-五华区教场东路莲花财富中心10楼;网课试听:ke.xuekaocn.cn   点击这里立即咨询我们



地州分校:   大理分校   丽江分校   迪庆分校   怒江分校   红河分校   临沧分校   玉溪分校   文山分校   保山分校   德宏分校   昭通分校   普洱分校   版纳分校 【各地州学员请加老师微信咨询报名,电话(微信):133-1252-4700】;2021年云南省成人高考>>立即报名



职业技能考证:心理咨询师、健康管理师、茶艺师等更多>◆咨询电话:133 1252 4700(微信)点击这里咨询我们  

公考培训咨询:◆国考云南省考公务员/事业单位面授培训,咨询电话:133 1252 4700 (微信)点击这里立即咨询我们






上一篇: Java设计模式:Java中的模式--单态3_
下一篇:没有了
温馨提示:因考试政策、内容不断变化与调整,学课在线网提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准!

免责声明:以上内容仅代表原创者观点,其内容未经本站证实,学课在线网对以上内容的真实性、完整性不作任何保证或承诺,转载目的在于传递更多信息,由此产生的后果与学课在线网无关;如以上转载内容不慎侵犯了您的权益,请联系我们QQ:1536696595,我们将会及时处理。




网友评论