java中单例模式的多种实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/true100/article/details/85599246

最近在复习JAVA知识,才发现我们平时使用的单例模式有多种实现,赶紧记录下,方便以后COPY.

  1. 测试类
/**
 * 单例的多种写法
 * @author Administrator
 *单例模式的优势:
 *对于项目中频繁使用的对象,可以节省创建对象的时间。
 *因为new的操作次数减少,对系统内存使用的频率也会减少,减少GC压力
 */

public class SingleInstanceDemo {
	public static void main(String[] args) {
		//饿汉模式
		Single4Hungry single4Hungry=Single4Hungry.getInstance();
		single4Hungry.getName();
		//懒汉模式
		Single4Lazy lazy=Single4Lazy.getInstance();
		lazy.getName();
		//懒汉线程安全模式
		Single4SafeLazy safeLazy=Single4SafeLazy.getInstance();
		safeLazy.getName();
		//DCL(双重检验模式)
		Single4DCL  dcl=Single4DCL.getInstance();
		dcl.getName();
		//静态内部类
		Single4StaticInnerClass innerClass=Single4StaticInnerClass.getInstance();
		innerClass.getName();
		//枚举
		Single4Enum enumInstance=Single4Enum.INSTANCE;
		enumInstance.getName();
	}
}

  • 第一种:饿汉模式
//饿汉模式缺点:不参为实例进行延迟加载
public class Single4Hungry {
	private static  final Single4Hungry instance=new Single4Hungry();
	public static Single4Hungry getInstance() {
		return instance;
	}
	//单例模式中的方法
	public void getName() {
		System.out.println(Single4Hungry.class.getSimpleName());;
	}
}
  • 第二种:懒汉模式
//懒汉模式缺点:在多线程并发情况下无法保证实例是唯一的
public class Single4Lazy {
	private static  Single4Lazy instance;
	private Single4Lazy() {
		
	}
	public static Single4Lazy getInstance() {
		if(null==instance) {
			instance=new Single4Lazy();
		}
		return instance;
	}
	//单例模式中的方法
	public void getName() {
		System.out.println(Single4Lazy.class.getSimpleName());
	}
}
  • 第三种:懒汉安全模式
//懒汉安全模式缺点:相对于懒汉模式更安全但是效率有所下降
public class Single4SafeLazy {
	private static  Single4SafeLazy instance;
	private Single4SafeLazy() {
		
	}
	//同步代码块实现线程安全(或者在获取实例的方法上添加synchronized关键字)
	public static Single4SafeLazy getInstance() {
		synchronized (Single4SafeLazy.class) {
			if(null==instance) {
				instance=new Single4SafeLazy();
			}	
		}
		
		return instance;
	}
	//单例模式中的方法
	public void getName() {
		System.out.println(Single4SafeLazy.class.getSimpleName());
	}
}
  • 第四种:双检查锁机制模式
//双检查锁机制模式(DCL):
public class Single4DCL {
	//使用volatile处理JVM的即时编译时存在的指令重排序问题
	private static volatile Single4DCL instance;
	private Single4DCL() {
		
	}
	//同步代码块实现线程安全(或者在获取实例的方法上添加synchronized关键字)
	public static Single4DCL getInstance() {
		//这里判空:就是为了不必要的同步
			if(null==instance) {
				//如果实例为null,则进行同步创建
				synchronized (Single4DCL.class) {
					if(null==instance) {
						instance=new Single4DCL();	
					}
				}
		}
		
		return instance;
	}
	//单例模式中的方法
	public void getName() {
		System.out.println(Single4DCL.class.getSimpleName());
	}
}
  • 第五种:
//静态内部类模式的单例:延迟加载,线程安全,性能好,
public class Single4StaticInnerClass {
	private Single4StaticInnerClass() {
	}
	
	public static Single4StaticInnerClass getInstance() {
		return SingleInner.instance;
	}
	//单例模式中的方法
	public void getName() {
		System.out.println(Single4StaticInnerClass.class.getSimpleName());
	}
	//静态内部类
	private static class SingleInner{
		private static final Single4StaticInnerClass instance=new Single4StaticInnerClass();
	}
}

  • 第六种:枚举类实现
//枚举类实现的单例:写法简单,线程安全
public enum Single4Enum {
	INSTANCE;
	//单例模式中的方法
	public void getName() {
		System.out.println(Single4Enum.class.getSimpleName());
}
}

猜你喜欢

转载自blog.csdn.net/true100/article/details/85599246
今日推荐