单例模式入门
单例模式
概述
确保单一个类一个进程仅有一个实例,并提供一个全局访问点,适用于工具类、配置管理、线程池等场景
避免重复创建对象浪费资源
不需要实例化该类的对象,不能使用new关键词创建对象
通常是自己创建自己的实例
与static的区别:
虽然static是实现单例的一种手段,
一个强调“共享性”
一个强调“唯一实例”
单例模式是控制类的实例数量仅1个,通过实例访问
static是类级别的共享资源,无需实例即可访问
单例模式在实例创建时开始,可手动销毁
static是随着类的加载而初始化,类的卸载而摧毁
可通过私有构造、成员变量实现封装
优点:
节省资源:对于数据库连接池,线程池等成本高的对象,避免重复创建对象,减少内存占用
全局一致性,同一个对象,避免数据不一致
通过getInstance()访问,可控,可添加控制逻辑(如线程安全检查)
作为全局访问点,方便不同模块之间共享数据或资源,无需通过参数传递对象
应用场景:
工具类:日志,配置,等
资源管理器:池
全局状态管理
当一个类的实例必须全局唯一、多实例会导致问题或浪费,且需要全局访问
饿汉式
类加载就会导致单实例对象创建
创建在JVM的方法区(jdk8后称“元空间”)
静态变量、常量、类的结构信息(类名、父类、接口、方法表等)
方法区的特点是:全局共享,所有线程共享
public class Singleton {private static Singleton INSTANCE = new Singleton();//类内部用new创建一次实例,随着类加载private Singleton() {//私有构造,禁止new}public static void Singleton//全局访问点:返回内部创建的实例public static Singleton getINSTANCE() {return INSTANCE;}
}
//代码块方式public Singleton() {}private static Singleton INSTANCE;static {INSTANCE = new Singleton();}
懒汉式
需要使用的时候才创建
所以会出现线程不安全,创建多个对象的问题
public Singleton() {}private static Singleton instance;
public static Singleton getInstance() {if(instance ==null){instance = new Singleton();}//需要的时候调用getinstance创建实例return instance;}
针对线程不安全所以要加个锁public static synchronized Singleton getInstance(){if(instance==null){instance=new Singleton();}
}
//将锁加在方法里面,也会创建多个实例
public Singleton() {}
private static Singleton instance;
public static Singleton getInstance() {if (instance == null) {synchronized (Singleton.class) {try {Thread.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}instance = new Singleton();}}return instance;}
//双重检查加锁
package test.Single;
public class Singleton {public Singleton() {}
private static Singleton instance;
public static Singleton getInstance() {if (instance == null) {synchronized (Singleton.class) {try {Thread.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}if(instance==null){instance = new Singleton();}
}}return instance;}
}
静态内部类创建实例
借助了静态内部类不会随着JVM加载类而加载,而是和外部类相互独立,使用内部类的时候才会被加载
此时JVM保证内部类的唯一性
public class Singleton {private Singleton() {}private static class staticSingleton{private static Singleton instance = new Singleton();
}public static Singleton getInstance(){return staticSingleton.instance;}
}
使用枚举类创建静态内部类public enum Singleton1 {INSTANCE;public static Singleton1 getInstance(){return INSTANCE;}
}