Foo
is thread safe once it has been safely published. For example, this program could print "unsafe" (it probably won't using a combination of hotspot/x86) - if you make bar
final it can't happen:
public class UnsafePublication {
static Foo foo;
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
while (foo == null) {}
if (!"abc".equals(foo.getBar())) System.out.println("unsafe");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
foo = new Foo("abc");
}
}).start();
}
}