当前位置:编程学习 > JAVA >>

编程题:singleton,实现延迟加载、泛型、和线程同步。

面试遇到的问题,本人小菜鸟,求大神指点,求注释。 singleton 线程 泛型 --------------------编程问答-------------------- 这么多你要问什么,先google下吧,应该就会有你要的答案。 --------------------编程问答--------------------

public class Singleton1 {

private Singleton1(){
System.out.println("This is Singleton1's instance.");
}

private static Singleton1 instance = null;

public static Singleton1 getInstance2() {
if(instance == null){   //1
synchronized (Singleton1.class) {  //2
if(instance == null) {
instance = new Singleton1();
}
}
}
return instance;
}

}
--------------------编程问答-------------------- 这类的问题。网上搜索下很多。  --------------------编程问答-------------------- 这种问题还是先百度吧,最好的理解方法不是死记,要自己去理解,理解了什么时候都不会忘记 --------------------编程问答--------------------
引用 2 楼 huxiweng 的回复:

public class Singleton1 {

private Singleton1(){
System.out.println("This is Singleton1's instance.");
}

private static Singleton1 instance = null;

public static Singleton1 getInstance2() {
if(instance == null){   //1
synchronized (Singleton1.class) {  //2
if(instance == null) {
instance = new Singleton1();
}
}
}
return instance;
}

}


差一个多态
是不是得定义一个父类了 --------------------编程问答-------------------- 去针对这几个题学习相应的知识啊,别人帮你把答案写出来,你不真正理解,有什么意思? --------------------编程问答--------------------
引用 2 楼 huxiweng 的回复:

public class Singleton1 {

private Singleton1(){
System.out.println("This is Singleton1's instance.");
}

private static Singleton1 instance = null;

public static Singleton1 getInstance2() {
if(instance == null){   //1
synchronized (Singleton1.class) {  //2
if(instance == null) {
instance = new Singleton1();
}
}
}
return instance;
}

}
+1
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,