Рассмотрим этот код:
public class World{
public static Point _point;
public static void main(String[] args){
new PointMaker().start();
System.out.println(_point);
}
}
public class Point{
private final int _x, _y;
public Point(int x, int y){
_x = x;
World._point = this;//BAD: publish myself before I'm fully constructed
//some long computation here
_y = y;
}
public void toString(){
return _x + "," + _y;
}
}
public class PointMaker extends Thread{
public void run(){
new Point(1, 1);
}
}
Поскольку Point
публикует себя перед установкой значения _y
, вызов println
может дать "1,0"
вместо ожидаемого "1,1"
.
(Обратите внимание, что это также может привести к "null"
, если PointMaker
+ Point.<init>
не зайдет достаточно далеко, чтобы установить поле World._point
до выполнения вызова println
.)