单例设计模式
饿汉式一(静态常量)
package com.singleton.type1;
/**
* @Classname Singleton01
* @Date 2019/12/14 0014 13:47
* @Created by Administrator
* @Description
*/
public class TestSingleton01 {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
//比较他们的哈希值是否相同
System.out.println("instance1.hashCode()=" + instance1.hashCode());
System.out.println("instance2.hashCode()=" + instance2.hashCode());
}
}
//饿汉式(静态变量)
class Singleton {
//1. 构造器私有化,外部不能通过new来创建对象
private Singleton() {
}
//2.本类内部创建对象实例
private final static Singleton instance = new Singleton();
//3. 提供一个公有的静态方法,返回实例对象
public static Singleton getInstance() {
return instance;
}
}
优缺点说明:
- 优点:写法简单,在类加载的时候就完成了实例化,避免了线程同步的问题
- 缺点:没有达到懒加载的效果,如果从始至终都没有用到这个实例,则会造成内存浪费
- 结论:这种单例模式可用,可能造成性能浪费
饿汉式二(静态代码块)
package com.singleton.type2;
/**
* @Classname TestSingleton02
* @Date 2019/12/14 0014 14:18
* @Created by Administrator
* @Description
*/
public class TestSingleton02 {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
//比较他们的哈希值是否相同
System.out.println("instance1.hashCode()=" + instance1.hashCode());
System.out.println("instance2.hashCode()=" + instance2.hashCode());
}
}
//饿汉式(静态变量)
class Singleton {
//1. 构造器私有化,外部不能通过new来创建对象
private Singleton() {
}
//2.本类内部创建对象实例
private final static Singleton instance;
static {
//在静态代码块中创建单例对象
instance = new Singleton();
}
//3. 提供一个公有的静态方法,返回实例对象
public static Singleton getInstance() {
return instance;
}
}
优缺点:
- 同上
懒汉式一(线程不安全)
package com.singleton.type3;
/**
* @Classname TestSingleton03
* @Date 2019/12/14 0014 14:29
* @Created by Administrator
* @Description
*/
public class TestSingleton03 {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
//比较他们的哈希值是否相同
System.out.println("instance1.hashCode()=" + instance1.hashCode());
System.out.println("instance2.hashCode()=" + instance2.hashCode());
}
}
class Singleton {
private static Singleton instance;
private Singleton() {
}
//提供一个静态的共有方法,当使用到该方法时,采取创建instance;
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
优缺点说明:
- 起到了懒加载(Lazy Loading)的效果,但是只能在单线程下使用,
- 在多线程下,如果一个线程进入了 if (instance == null) 判断语句块,还未来得及往下执行,另外一个线程也通过了这个判断语句,这时候就会产生多个实例,所以在多线程下不可以使用这种方式;
- 结论:在开发中,不要使用这种方式。
懒汉式二(线程安全)
package com.singleton.type4;
/**
* @Classname TestSingleton04
* @Date 2019/12/14 0014 14:42
* @Created by Administrator
* @Description
*/
public class TestSingleton04 {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
//比较他们的哈希值是否相同
System.out.println("instance1.hashCode()=" + instance1.hashCode());
System.out.println("instance2.hashCode()=" + instance2.hashCode());
}
}
class Singleton {
private static Singleton instance;
private Singleton() {
}
//提供一个静态的共有方法,当使用到该方法时,采取创建instance;
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
特点:
- 解决了线程安全问题,但是效率低,不推荐使用
懒汉式三(线程安全,同步代码块,不能用)
这个是不能解决线程安全的问题,没有研究价值,直接不能用
//提供一个静态的共有方法,当使用到该方法时,采取创建instance;
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class){
instance = new Singleton();
}
}
return instance;
}
双重检查
package com.singleton.type6;
class Singleton {
private static volatile Singleton instance;
private Singleton() {
}
//提供一个静态的共有方法,当使用到该方法时,采取创建instance;
public static synchronized Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
静态内部类
package com.singleton.type7;
class Singleton {
private static volatile Singleton instance;
private Singleton() {
}
//写一个静态内部类该类中有一个静态属性SIngleton;
private static class SingletonInstance{
private static final Singleton INSTANCE = new Singleton();
}
//提供一个静态的共有方法,当使用到该方法时,采取创建instance;
public static synchronized Singleton getInstance() {
return SingletonInstance.INSTANCE;
}
}
优缺点说明:
- 静态内部类利用了jvm底层提供的类装载机制保证了对象初始化时的线程安全,因此静态内部类的形式下的单例推荐使用;
- 静态内部类方式在Singleton类装载时,并不会被实例化,而是在需要实例化的时候,调用getInstance(),才会装载SingletonInstance类,从而完成Singleton 的实例化;
- 类的静态属性只会在第一次加载类的时候初始化,(独一份),所以这里JVM帮我们保证了线程安全,在类初始化时,别的线程时无法进入的;
- 优点:避免了线程安全,利用静态内部类的特点实现延迟加载,效率高;
- 结论:推荐使用。
枚举下的单例(推荐使用)
package com.singleton.type8;
/**
* @Classname TestSingleton
* @Date 2019/12/14 0014 15:12
* @Created by Administrator
* @Description
*/
public class TestSingleton {
public static void main(String[] args) {
Singleton instance1 = Singleton.INSTANCE;
Singleton instance2 = Singleton.INSTANCE;
System.out.println(instance1 == instance2);
//比较他们的哈希值是否相同
System.out.println("instance1.hashCode()=" + instance1.hashCode());
System.out.println("instance2.hashCode()=" + instance2.hashCode());
instance1.sayOK();
}
}
enum Singleton {
INSTANCE;
public void sayOK(){
System.out.println("ok```");
}
}
// enum 是一个什么? 本身也是一个Class类
public enum EnumSingle {
INSTANCE;
public EnumSingle getInstance(){
return INSTANCE;
}
}
特点:
- 借助java5中添加的枚举来实现单例模式,不仅能避免线程同步的问题,而且还能防止反序列化重新创建新的对象。
- 结论:推荐使用。
总结:
推荐使用的有:双重检查、静态内部类、枚举(多线程)
饿汉式(单线程)
单例模式注意事项和细节说明
- 单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建、销毁的对象,使用单例模式可以提高系统性能;
- 当想实例化一个单例的时候,必须要记住使用相应的获取对象的方法,而不是使用new;
- 单例模式的使用场景:需要频繁的创建、销毁的对象,创建对象时耗时过多、或耗费资源过多(即:重量级对象),但又经常用到的对象、工具类对象、频繁访问数据库或文件的对象(比如:数据源、session工厂等)