Java???????????????
???????????? ???????[ 2014/5/22 11:32:36 ] ????????Java ??????????
??????????
????synchronized(this)??synchronized(MyClass.class)??????????synchronized??????????????????synchronized????????????
????synchronized???????ó????????????????????
public class Singleton {
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
|
??????????????????????????????????й?????????????????????????????????????????°??java?????????
???????????????????????????????????????????????????????????????????????????????????????????????飩
//???????????????????
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
|
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
|
????volatile????????????????е????????????????????????????????????????
??????synchronized???α?????get??set??????????????????volatile???α????????Ч?????????????????synchronized???????????????????????????????????棬??????????ж????????±????????????synchronized???л????Ч??????????Ч???????????????????synchronized??????????????С??????????????????????volatile??????volatile??????????????á?
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11