• 单例模式


    单例模式的关键点

    1) 构造方法不对外开放,为private(调用不能用new)

    2) 确保单例类只有一个对象,尤其是多线程模式下

    3) 通过静态方法或枚举返回单例对象

    4) 确保单例类在反序列化是不会重新创建新的对象

    单例模式的实现方式

    1) 饿汉式

    public class Singleton1 {

    /*

    * 饿汉式是在声明的时候就已经初始化Singleton1,确保了对象

    的唯一性

    *

    * 声明的时候就初始化对象会浪费不必要的资源

    * */

    private static Singleton1 instance = new Singleton1();

    private Singleton1() {

    }

    // 通过静态方法或枚举返回单例对象

    public Singleton1 getInstance() {

    return instance;

    }

    }

    2) 懒汉式

    public class Singleton2 {

    private static Singleton2 instance;private Singleton2() {

    }

    /*

    * getInstance 添加了synchronized 关键字,,也就是说

    getInstance 是一个同步方法,

    * 这就是懒汉式在多线程中保持唯一性的方式

    *

    * 懒汉式存在的问题是,即是instance已经被创建,每次调用

    getInstance方法还是会同步,这样就会浪费一些不必要的资源

    * */

    public static synchronized Singleton2 getInstance() {

    if (instance == null) {

    instance = new Singleton2();

    }

    return instance;

    }

    }

    双重检查

    线程安全式中,同步了整个getInstance方法才保证了线程安全,会浪

    费很多性能。于是我们可以使用双重检查式,它将只在初次创建实例的

    时候实现同步。(了解锁优化的同学可以理解为减少锁的颗粒度)

    public class SingletonLH {

    private static SingletonLH singleton = null;

    private SingletonLH() {}

    public static SingletonLH getSingleton1() throws

    InterruptedException {

    if (singleton == null) {

    synchronized (SingletonLH.class) {

    if (singleton == null) {

    singleton = new

    SingletonLH();

    }

    }

    }

    return singleton;

    }

    }

  • 相关阅读:
    ftp服务器架设
    samba服务器架设
    apache安装
    yum及prm安装
    redis安装与使用
    memcached安装与使用
    mysql主从服务器
    nginx负载均衡服务器搭建
    lnmp环境搭建
    linux笔记
  • 原文地址:https://www.cnblogs.com/ycq-qiang/p/11161988.html
Copyright © 2020-2023  润新知