标签:vol 静态内部类 空指针 通过 四种 单例模式 final static sync
1.饿汉式
public class DownLoad{
private static final DownLoad mDownload =new DownLoad();
private download(){
}
public static DownLoad getInstance(){
return mDownload ;
}
}
2.懒汉式
public class DownLoad{
private static final DownLoad mDownload ;
private download(){
}
public static DownLoad getInstance(){
if(mDownload ==null){
mDownload =new DownLoad();
}
return mDownload ;
}
}
3.double check(会出现空指针问题,可通过添加volatile关键字解决)
public class DownLoad{
private static volatile DownLoad mDownload ;
private download(){
}
public static DownLoad getInstance(){
if(mDownload ==null){
synchronized(DownLoad.class){
if(mDownload ){
mDownload =new DownLoad();
}
}
}
return mDownload ;
}
}
4.静态内部类
public class DownLoad{
public static class{
private static DownLoad mDownload=new DownLoad();
public static DownLoad getInstance(){
return mDownload;
}
}
}
标签:vol 静态内部类 空指针 通过 四种 单例模式 final static sync
原文地址:http://www.cnblogs.com/zql5211/p/7417016.html