o*******a 发帖数: 46 | 1 public class Foo{
private static Foo mInstance;
public static Foo getInstance(){
if(mInstance == null){
synchronized(Foo.class){
if(mInstance == null){
mInstance = new Foo();
}
}
}
return mInstance;
}
}
Will this work in all environments? If not, under what condition will this
fail? ? |
xt 发帖数: 17532 | 2 I don't see any problem with it. |
s*****g 发帖数: 1 | 3 make default constr private
【在 o*******a 的大作中提到】 : public class Foo{ : private static Foo mInstance; : : public static Foo getInstance(){ : if(mInstance == null){ : synchronized(Foo.class){ : if(mInstance == null){ : mInstance = new Foo(); : } : }
|
m******t 发帖数: 2416 | 4 Google "Double Checked Locking Is Broken".
【在 o*******a 的大作中提到】 : public class Foo{ : private static Foo mInstance; : : public static Foo getInstance(){ : if(mInstance == null){ : synchronized(Foo.class){ : if(mInstance == null){ : mInstance = new Foo(); : } : }
|
c*****t 发帖数: 1879 | 5 Good reading. Never realized that such tiny problem could have
such complicated answer :) Re-ordering can be so evil.
【在 m******t 的大作中提到】 : Google "Double Checked Locking Is Broken".
|