跳转到内容

欢迎来到Riguz的小站!这是一个私人wiki,用来记录一些我的笔记。

Java this escape

来自WHY42
Riguz​(留言 | 贡献)2021年5月1日 (六) 13:53的版本 (建立內容為「<syntaxhighlight lang="java"> public FinalFieldExample() { // bad! x = 3; y = 4; // bad construction - allowing this to escape global.obj = this; } </synt…」的新頁面)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
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.