大家好,又见面了,我是你们的朋友全栈君。
1、单例模式(Singleton Pattern):确保某一个类最多只有一个实例,并向整个系统提供这个实例,即该类需提供一个访问唯一实例的全局方法,这个类称为单例类。单例模式的目的是使得某个类最多只有一个实例。
2、为了确保单例类最多只有一个实例,且能够向外部提供唯一实例,单例类应具备以下特点特征:(1)构造方法私有化;(2)能够生成唯一实例;(3)存在能够向外部提供唯一实例的方法;(4)实例和方法需用static关键词修饰。
3、单例模式确保了系统中只能存在唯一实例,则在内存里只有一个实例,这样在频繁的创建和销毁实例时可以减少内存的开销。但由于单例类只向外部提供了访问实例的方法、没有接口,无法被重用和扩展。
单例设计模式主要解决的是类的频繁创建与销毁问题,通过控制类实例的创建来节省系统资源。
运用场景很多,例如网站的在线人数,window系统的任务管理器,网站计数器等等,这些都是单例模式的运用。单例模式有常见的8种形式,如下:
public class Singleton_Lazy1 {
private Singleton_Lazy1() {
};
private static Singleton_Lazy1 instance = null;
public static Singleton_Lazy1 getInstance() {
if (instance == null) {
instance = new Singleton_Lazy1();
}
return instance;
}
}
public class Singleton_Lazy2 {
private Singleton_Lazy2() {
};
private static Singleton_Lazy2 instance = null;
public static synchronized Singleton_Lazy2 getInstance() {
if (instance == null) {
instance = new Singleton_Lazy2();
}
return instance;
}
}
public class Singleton_DCL1 {
private Singleton_DCL1() {
};
private static Singleton_DCL1 singleton;
public static Singleton_DCL1 getInstance() {
if (singleton == null) {
synchronized (Singleton_DCL1.class) {
if (singleton == null) {
singleton = new Singleton_DCL1();
}
}
return singleton;
}
}
}
双重检测机制:
延迟初始化
多线程不安全
线程稳定
1.为了防止new Singleton被执行多次,因此在new操作之前加上Synchronized 同步锁,锁住整个类(注意,这里不能使用对象锁)。
2.进入Synchronized 临界区以后,还要再做一次判空。因为当两个线程同时访问的时候,线程A构建完对象,线程B也已经通过了最初的判空验证,不做第二次判空的话,线程B还是会再次构建instance对象。这种同步并不能起到线程同步的作用
但是这样的双重检测机制仍然不是绝对线程安全!这里涉及到JVM编译器的指令重排。
一般创建一个对象的时候会有三个步骤:
instance = new Singleton
Value =allocate(); //1:分配对象的内存空间
ctorInstance(Value); //2:初始化对象
instance =Value; //3:设置instance指向刚分配的内存地址
但这三步并不是固定不变的,有可能会经过JVM和CPU的优化,指令将会重排为:
instance = new Singleton
Value =allocate(); //1:分配对象的内存空间
instance =Value; //3:设置instance指向刚分配的内存地址
ctorInstance(Value); //2:初始化对象
当线程A执行完1、3时,instance对象还未完成初始化,但是已经不指向null。这个时候如果B线程进入CPU,则会抢先执行if(instance == null)的判断结果为false,这个时候getInstance方法则会返回一个没有初始化完成的instance对象,结果如下图:
public class Singleton_DCLPro {
private Singleton_DCLPro() {
};
private volatile static Singleton_DCLPro singleton;
public static Singleton_DCLPro getInstance() {
if (singleton == null) {
synchronized (Singleton_DCLPro.class) {
if (singleton == null) {
singleton = new Singleton_DCLPro();
}
}
}
return singleton;
}
}
public class Singleton_Hunger1 {
private final static Singleton_Hunger1 INSTANCE = new Singleton_Hunger1();
private Singleton_Hunger1() {
};
public static Singleton_Hunger1 getInstance() {
return INSTANCE;
}
}
public class Singleton_Hunger2 {
private static Singleton_Hunger2 instance;
static {
instance = new Singleton_Hunger2();
}
private Singleton_Hunger2() {
};
public Singleton_Hunger2 getInstance() {
return instance;
}
}
饿汉式(静态代码块):
线程稳定
不会延迟初始化
多线程安全
优缺点与Hunger1一样。
这种方式和上面的方式其实类似,只不过将类实例化的过程放在了静态代码块中,也是在类装载的时候,就执行静态代码块中的代码,初始化类的实例。
static{
instance = new Singleton_Hunger2;
}
public class Singleton_Pattern {
private Singleton_Pattern() {
};
private static class SingletonInstance {
private static final Singleton_Pattern INSTANCE = new Singleton_Pattern();
}
public Singleton_Pattern getInstance() {
return SingletonInstance.INSTANCE;
}
}
public enum Singleton_E {
INSTANCE;
public void whateverMethod() {
System.out.println("这是一个枚举单例");
}
}
不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。代码简洁。使用起来方便。
synchronized的使用
在访问volatile变量时不会执行加锁操作,因此也就不会使执行线程阻塞,因此volatile变量是一种比sychronized关键字更轻量级的同步机制。
volatile关键字是防止创建对象时的重排序,在访问volatile变量时不会执行加锁操作。
volatile关键字不但可以防止指令重排,也可以保证线程访问的变量值是主内存中的最新值。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/169699.html原文链接:https://javaforall.cn