Java this escape

来自WHY42
Riguz讨论 | 贡献2023年12月19日 (二) 06:55的版本 (Riguz移动页面Java:this逸出Java this escape,不留重定向)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
public FinalFieldExample() { // bad!
  x = 3;
  y = 4;
  // bad construction - allowing this to escape
  global.obj = this;
}

then threads that read the reference to this from global.obj are not guaranteed to see 3 for x.