• 单例模式(饿汉式和懒汉式 )(简洁版)(Java)


    一. 单例模式的简单介绍

            单例模式是设计模式的一种,设计模式就是在大量的实践中总结的代码结构,编程风格,以及解决问题的思考方式。

            所谓的单例模式就是对于某个类只能存在一个对象实例,并且该类只提供一个取得对象实例的方法。如果我们让一个类只产生一个对象,我们首先要设置类的构造器是私有的。这样,在类的外部就没有办法随便造类了。但是在类的内部仍然可以创建对象。因为在类的外部无法获得对象,所以必须让这个类的唯一对象是静态的,这样,在类初始化时就可以跟随着类一起被生成。

    二. 单例模式的类型

            单例模式有两种类型,懒汉式和饿汉式。

    1. 懒汉式

            懒汉式的懒体现在啥时候用啥时候造,也就是刚开始为null。

    2. 饿汉式

            比较饿,饥渴,上来先造对象。

    3. 两者的区别

            (1)懒汉式比较好一点,延迟去创建对象,饿汉式提前加载号对象,但是一直不用,占用资源,生命周期过长。

            (2)饿汉式是线程安全的,天然线程安全,懒汉式在if(instance==null)处可能会出现线程不安全的情况

    三. 单例模式的代码

    1. 懒汉式

    1. package TestOnly;
    2. public class SingleTonTest {
    3. public static void main(String[] args) {
    4. Lazy instance3 = Lazy.getinstance();
    5. Lazy instance4 = Lazy.getinstance();
    6. System.out.println(instance3 == instance4);
    7. }
    8. }
    9. class Lazy{
    10. //1.私有化类的构造器
    11. private Lazy(){
    12. }
    13. //4.声明为private
    14. //2.声明对象,但是没有new
    15. private static Lazy instance = null;
    16. public static Lazy getinstance(){
    17. if(instance == null){
    18. instance = new Lazy();
    19. }
    20. return instance;
    21. }
    22. }

    2. 饿汉式

    1. package TestOnly;
    2. public class SingleTonTest {
    3. public static void main(String[] args) {
    4. Hungry instance1 = Hungry.getintance();
    5. Hungry instance2 = Hungry.getintance();
    6. System.out.println(instance1 == instance2);
    7. }
    8. }
    9. class Hungry{
    10. //1.构造器私有
    11. private Hungry(){
    12. }
    13. //4.声明为static
    14. //2.获得该类的对象声明为私有的
    15. private static Hungry instance = new Hungry();
    16. //3.返回方法
    17. public static Hungry getintance(){
    18. return instance;
    19. }
    20. }

     

    3. 懒汉式线程安全版1

    1. class LazySafe1{
    2. private LazySafe1(){
    3. }
    4. private static LazySafe1 instance = null;
    5. public static synchronized LazySafe1 getinstance(){
    6. if(instance == null){
    7. instance = new LazySafe1();
    8. }
    9. return instance;
    10. }
    11. }

     

    4. 懒汉式线程安全版2

    1. class LazySafe2{
    2. private LazySafe2(){
    3. }
    4. private static LazySafe2 instance = null;
    5. public static LazySafe2 getinstance(){
    6. synchronized(LazySafe2.class){
    7. if(instance == null){
    8. instance = new LazySafe2();
    9. }
    10. return instance;
    11. }
    12. }
    13. }

     

    5. 懒汉式线程安全版快速版

    1. class LazySafe3{
    2. private LazySafe3(){
    3. }
    4. private static LazySafe3 instance = null;
    5. public static LazySafe3 getinstance(){
    6. if(instance == null){
    7. synchronized(LazySafe3.class){
    8. if(instance == null){
    9. instance = new LazySafe3();
    10. }
    11. }
    12. }
    13. return instance;
    14. }
    15. }

    6. 三种线程安全版的懒汉式的区别

             第一种和第二种是一样的,区别在于用什么方式实现锁,一个是同步代码块,拿类的class充当唯一对象,另外一个是用的同步方法。而四三种方法则是预先判断该对象有没有被创建出来,提前判断,而节省了拿锁排队等待的情况。

    参考来源:【1】bilibili 尚硅谷 尚硅谷Java入门视频教程(在线答疑+Java面试真题)

  • 相关阅读:
    C 和 C++ 中主函数调用其他文件中的函数,实现代码重用
    C语言:如何给全局变量起一个别名?
    Stm32_标准库_7_光敏传感器
    【C++】---STL之list详解
    接口测试面试题整理​​​​​​​
    X11 Xlib截屏问题及深入分析四 —— XOpenDisplay函数源码分析(1)
    Vue 官方文档2.x教程学习笔记 1 基础 1.6 Class 与 Style 绑定 1.6.2 绑定内联样式
    一文理解OpenStack网络
    学习笔记-ThinkPHP 之 SQLI审计分析(三)
    Java并发编程的艺术笔记-Executor框架
  • 原文地址:https://blog.csdn.net/weixin_45532984/article/details/126898695