“学习“java之单例模式

饿汉式:
public User(){
private static User s=new User();
private User(){
}
public static User getUser()
{
return s;
}
}
懒汉式:
class User{
private static User s=null;
public User(){
if(s==null)
s=new User();
return s;
}
}
同步锁下的懒汉式:
class User{
private static User s=null;
public User(){

public static Single1 getInStanceBlock(){
if(s==null)
synchronized (User.class) {
if(s==null)
s = new User();
}
return s;
}



}

你可能感兴趣的:(学习java)