[align=left][/align]简单单例模式:
public class danli(){
private danli(){
}
private static Test t = new Test();
public static Test getT(){
return t;
}
}
这种方法创建的单例缺点就是不能解决多线程问题!
懒汉式:
public class danli(){
private danli(){
}
private static Test t =null;
public static synchronized Test getT(){
if(t == null){
t = new Test();
}
return t;
}
}
这种方法解决咯上面多线程的问题!但是还存在一个性能的问题!当多个线程同时访问时候,进入方法的线程独占资源,后面的线程将一直等待;
双锁机制:
public class danli(){
private danli(){
}
private static Test t =null;
public static Test getT(){
if(t == null){
synchronized(Test.class){
t = new Test();
}
}
return t;
}
}
这种方法很好的解决咯上面的问题性能提高,支持高并发量;