Brain Goetz による安全な構築手法に関するこの優れた記事を読んで、リスト 5 内にあるコメントに混乱しました。コード スニペットは次のとおりです。
public class Safe {
private Object me;
private Set set = new HashSet();
private Thread thread;
public Safe() {
// Safe because "me" is not visible from any other thread
me = this;
// Safe because "set" is not visible from any other thread
set.add(this);
// Safe because MyThread won't start until construction is complete
// and the constructor doesn't publish the reference
thread = new MyThread(this);
}
public void start() {
thread.start();
}
private class MyThread(Object o) {
private Object theObject;
public MyThread(Object o) {
this.theObject = o;
}
...
}
}
なぜ彼はこれについて言うのme
ですか?// Safe because "me" is not visible from any other thread
. 2 つのスレッドが同時にインスタンス変数にアクセスできませんme
か?