public class Instance1 {
private static Instance instance;
private Instance () {}
public static Instance getInstance () {
if (this.instance == null) {
instance = new Instacne();
}
return this.instance;
}
}
/**
* 多线程安全单例模式
*
*/
public class Instance2 {
private static Instance instance;
private Instance () {}
public static Instance getInstance () {
if (this.instance == null) {
synchornized(Instance2.class) {
if (this.instance == null) {
instance = new Instacne();
}
}
}
return this.instance;
}
}
Java单例模式(面试)
最新推荐文章于 2023-12-22 17:38:45 发布