zoukankan      html  css  js  c++  java
  • JAVA之单例模式

    好久不见小伙伴们,最近在开发中想把以前写的一些代码进行下优化,正好遇到了一些可以使用到单例模式的代码,趁此机会就对单例模式进行下代码整理,听说有8种写法,我只会2种,哭泣~

    先来科普下什么是单例模式:

    单例模式,是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中,应用该模式的类一个类只有一个实例。即一个类只有一个对象实例。

    怎么实现

    1. 将该类的构造方法定义为私有方法,这样就可以避免其他类对其进行实例化。
    2. 在该类内部提供一个静态方法待调用实例对象。

    有什么好处

    因为只有一个对象,避免了新增和销毁对象时的开销,节省资源提高性能。

    写法科普

    先放结论:123为最基础的,但线程不安全;45线程安全,但不推荐用;678推荐用。

    1. 饿汉式(静态常量)

      // 整个类中,除了getInstance()是共有的,其他都是私有的
      public class Singleton {
      	// 注意前面的三个关键词,一定要是私有静态常量
          private final static Singleton INSTANCE = new Singleton();
          // 注意此处的构造方法是私有的,不是通常的共有
          private Singleton(){}
          public static Singleton getInstance(){
              return INSTANCE;
          }
      }
      

      优点:这种写法比较简单,就是在类装载的时候就完成实例化。避免了线程同步问题。

      缺点:在类装载的时候就完成实例化,没有达到Lazy Loading的效果。如果从始至终从未使用过这个实例,则会造成内存的浪费。

    2. 饿汉式(静态代码块)

      public class Singleton {
      
          private static Singleton instance;
          static {
              instance = new Singleton();
          }
          private Singleton() {}
          public static Singleton getInstance() {
              return instance;
          }
      }
      

      和上面的不同点在于实例化放到了静态代码块,在类加载的时候就会实例化。

    3. 懒汉式(线程不安全)

      public class Singleton {
      
          private static Singleton singleton;
          private Singleton() {}
          public static Singleton getInstance() {
              // 线程不安全的原因就是因为下面这个if判断
              // 一个线程进入了判断语句块,另一个线程也通过了这个判断语句,这时便会产生多个实例
              if (singleton == null) {
                  singleton = new Singleton();
              }
              return singleton;
          }
      }
      
    4. 懒汉式(使用同步方法实现线程安全,但不推荐用)

      public class Singleton {
          private static Singleton singleton;
          private Singleton() {}
          public static synchronized Singleton getInstance() {
              if(singleton == null) {
                  singleton = new Singleton();
              }
              return singleton;
          }
      }
      

      效率太低。

    5. 懒汉式(使用同步代码块实现线程安全,不可用)

      public class Singleton {
          private static Singleton singleton;
          private Singleton() {}
          public static Singleton getInstance() {
              if (singleton == null) {
                  synchronized (Singleton.class) {
                      singleton = new Singleton();
                  }
              }
              return singleton;
          }
      }
      

      和4一样加了个锁,只不过位置换了下,效率变好了一些,但是会出现3的那种问题。

    6. 双重检查

      public class Singleton {
          private static volatile Singleton singleton;
          private Singleton() {}
          public static Singleton getInstance() {
              if (singleton == null) {
                  synchronized (Singleton.class) {
                      if (singleton == null) {
                          singleton = new Singleton();
                      }
                  }
              }
              return singleton;
          }
      }
      

      双重加锁,在5的基础上进行改善,对singleton变量也加了个锁,避免了5的问题,可以保证线程安全。

    7. 静态内部类

      public class Singleton {
          private Singleton() {}
          private static class SingletonInstance {
              private static final Singleton INSTANCE = new Singleton();
          }
          public static Singleton getInstance() {
              return SingletonInstance.INSTANCE;
          }
      }
      

      使用了类加载的机制来保证初始化实例的时候只有一个线程,同时避免了类被装载的时候就立即实例化的问题,只有当需要的时候,才会进行实例化。

    8. 枚举

      public enum Singleton {
          INSTANCE;
          public void whateverMethod() {
              
          }
      }
      

      借助了枚举的特性,不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。

  • 相关阅读:
    JDK15视频会及新特性总节
    设计模式之访问者模式
    datax分析与思考(一)
    beanfactory中单例bean的初始化过程(一)
    IIS 404错误,错误代码:0x80070002
    WebApi
    多线程--程序员必修课
    委托(续2)
    委托(续)
    委托
  • 原文地址:https://www.cnblogs.com/yisany/p/11128614.html
Copyright © 2011-2022 走看看