当前位置: > 财经>正文

什么是单例模式?有什么作用和特点?可以解决哪些问题?懒汉式和饿汉式的区别?如何保证线程安全?

2023-07-17 04:50:47 互联网 未知 财经

什么是单例模式?有什么作用和特点?可以解决哪些问题?懒汉式和饿汉式的区别?如何保证线程安全?

单例模式:

一个类只允许创建一个实例对象,并提供访问其唯一的对象的方式。这个类就是一个单例类,这种设计模式叫作单例模式。

 

作用:

避免频繁创建和销毁系统全局使用的对象。

 

单例模式的特点:

单例类只能有一个实例单例类必须自己创建自己的唯一实例单例类必须给所有其他对象提供这一实例的访问

 

应用场景:

全局唯一类,如 系统配置类、系统硬件资源访问类序列号生成器Web 计数器

 

饿汉式与懒汉式的区别:

饿汉式是类一旦加载,就把单例初始化完成,保证 getInstance() 方法被调用时的时候,单例已经初始化完成,可以直接使用。懒汉式比较懒,只有当被调用 getInstance() 方法时,才会去初始化这个单例。

 

线程安全性问题:

饿汉式,在被调用 getInstance() 方法时,单例已经由 jvm 加载初始化完成,所以并发访问 getInstance() 方法返回的都是同一实例对象,线程安全。

懒汉式,要保证线程安全,可以有以下几种方式:

给静态 getInstance() 方法加锁,性能差getInstance() 方法双重检查给类加锁后创建对象(以上两种低版本 JDK,由于指令重排,需要加 volatile 关键字,否则创建出多个对象;JDK 1.5 内存模型加强后解决了对象 new 操作和初始化操作的原子性问题)通过静态内部类实现通过枚举实现

示例代码: 1、饿汉式

package constxiong.interview;/** * 单例模式 饿汉式 * @author ConstXiong */public class TestSingleton {    private static final TestSingleton instance = new TestSingleton();        private TestSingleton() {    }        public static TestSingleton getInstance() {        return instance;    }    }

2、懒汉式:线程不安全

package constxiong.interview;/** * 单例模式 懒汉式-线程不安全 * @author ConstXiong */public class TestSingleton {    private static TestSingleton instance;        private TestSingleton() {    }        public static TestSingleton getInstance() {        if (instance == null) {            instance = new TestSingleton();        }        return instance;    }    }

3、懒汉式:getInstance() 方法加锁,线程安全,性能差

package constxiong.interview;/** * 单例模式 懒汉式-加锁 * @author ConstXiong */public class TestSingleton {    private static volatile TestSingleton instance;        private TestSingleton() {    }        public static synchronized TestSingleton getInstance() {        if (instance == null) {            instance = new TestSingleton();        }        return instance;    }    }

4、懒汉式:双重检查 + 对类加锁

package constxiong.interview;/** * 单例模式 懒汉式-双重检查 + 对类加锁 * @author ConstXiong */public class TestSingleton {    private static volatile TestSingleton instance;        private TestSingleton() {    }        public static TestSingleton getInstance() {        if (instance == null) {            synchronized (TestSingleton.class) {                if (instance == null) {                    instance = new TestSingleton();                }            }        }        return instance;    }    }

5、懒汉式:静态内部类

package constxiong.interview;/** * 单例模式 懒汉式-静态内部类 * @author ConstXiong */public class TestSingleton {    private static class SingletonHolder {        private static final TestSingleton instance = new TestSingleton();    }        private TestSingleton() {    }        public static TestSingleton getInstance() {        return SingletonHolder.instance;    }    }

6、懒汉式:枚举

package constxiong.interview;import java.util.concurrent.atomic.AtomicLong;/** * 单例模式 懒汉式-枚举,id生成器 * @author ConstXiong */public enum TestSingleton {    INSTANCE;    private AtomicLong id = new AtomicLong(0);    public long getId() {        return id.incrementAndGet();    }}

 

实现方式的选择建议:

没有特殊要求,建议使用 1、饿汉式,提前初始化好对象,虽然提前占用内存资源和提前了初始化的时间,但避免了懒加载过程中程序出现内存不够、超时等问题,符合 fail-fast 原则。明确要求懒加载,可以使用 5、静态内部类的方式有其他特殊要求,使用 4、双重检查 + 对类加锁的方法

 

 

【Java面试题与答案】整理推荐 基础与语法集合网络编程并发编程Web安全设计模式框架算法与数据结构异常文件解析与生成LinuxMySQLOracleRedisDubbo

 

版权声明: 本站仅提供信息存储空间服务,旨在传递更多信息,不拥有所有权,不承担相关法律责任,不代表本网赞同其观点和对其真实性负责。如因作品内容、版权和其它问题需要同本网联系的,请发送邮件至 举报,一经查实,本站将立刻删除。