单例模式(singleton):是JAVA中最简单的一种设计模式,属于建立型模式。所谓单例,就是整个程序有且仅有一个实例。设计模式
特色:安全
构造方法私有化多线程
在本类中实例化一个对象做为本类的属性并发
对外提供一个访问本类对象的方法高并发
饿汉式:类加载时就加载对象性能
应用场景:小对象,频繁用,高并发spa
特色:线程安全,比较经常使用,但容易产生垃圾,影响性能,由于一开始就初始化。线程
1 class Singleton{ 2 //构造方法私有化 3 private Singleton() { 4 System.out.println("构造方法"); 5 } 6 //对象在类加载时初始化 7 private static final Singleton instance = new Singleton(); 8 //提供对外的访问方法 9 public static Singleton getInstance() { 10 return instance; 11 } 12 }
懒汉式:对象什么时候须要什么时候建立,线程不安全设计
应用场景:单线程,大对象code
特色:线程不安全,延迟初始化。
1 class Singleton{ 2 private Singleton() { 3 System.out.println("构造方法"); 4 } 5 private static Singleton instance; 6 public static Singleton getInstance() { 7 if (instance == null) { 8 instance = new Singleton(); 9 } 10 return instance; 11 } 12 }
同步锁机制
应用场景:多线程,大对象,稀少用。
特色:经过加锁保证了线程安全,性能会降低。
1 class Singleton{ 2 private Singleton() { 3 System.out.println("构造方法"); 4 } 5 private static Singleton instance; 6 //同步方法,线程安全,但性能会降低 7 public static synchronized Singleton getInstance() { 8 if (instance == null) { 9 instance = new Singleton(); 10 } 11 return instance; 12 } 13 }
双重验证机制
应用场景:大对象,稀少用,并发量不能太大
特色:线程安全,延迟初始化。
1 class Singleton{ 2 private Singleton() { 3 System.out.println("构造方法"); 4 } 5 private static volatile Singleton instance; 6 //同步方法,双重验证,减小阻塞次数,提升性能 7 public static Singleton getInstance() { 8 if (instance == null) { 9 synchronized (Singleton.class) { 10 if (instance == null) { 11 instance = new Singleton(); 12 } 13 } 14 } 15 return instance; 16 } 17 }
静态内部类
引用场景:大对象,频繁用,高并发
特色:延时对象建立,减小资源占用,提升系统性能
1 class Singleton{ 2 private Singleton() { 3 System.out.println("构造方法"); 4 } 5 static class Inner{ 6 private static final Singleton instance = new Singleton(); 7 } 8 public static Singleton getInstance() { 9 return Inner.instance; 10 } 11 }
枚举
1 enum Singleton{ 2 //类加载时建立 3 INSTANCE; 4 }
因为单例模式是建立型模式,每次调用都会新建一个实例。那么一个重要的问题就是反序列化。当实例被写入到文件到反序列化成实例时,咱们须要重写readResolve
方法,以让实例惟一。
1 private Object readResolve() throws ObjectStreamException{ 2 return singleton; 3 }