Java ~ singleton design pattern

Micro-channel public number: programmer Hotel
focus on sharing technology programmer dry goods, including development tools, Java foundation, Java concurrency, Python, JVM, distributed, micro-services, message queues, Git, the source code parsing, database, design patterns, interview by machine , life and other programs to help you program the road detours.
Thanks for reading, welcome attention

Singleton

Defined singleton (the Singleton) mode: a class and only one example, and examples of their own available to the entire system. For example, Windows can only open a task manager, which can be avoided by opening multiple windows task manager and a waste of memory resources, or display the contents of each window appears inconsistent and so wrong.

In a computer system, as well as connection pooling Windows Recycle Bin, operating system, file system, multiple threads in the thread pool, the graphics card driver object, the printer spooler service, the application's log objects, database, website counter, configuration object Web application, the application dialog system caching often designed as a single embodiment.

Singleton has three characteristics:
- a singleton instance of the object only;
- the object must be created by a single embodiment singleton class itself;
- singleton global provide external access to a single access point to the embodiment;

1, single cases of hunger

It features of the model: Once loaded class creates a singleton to ensure that before calling the method getInstance Singleton has been in existence.

Are Lazy Initialization: No
whether the multi-thread safe: is
difficult to achieve: a simple
advantage: not locked, the efficiency will improve.
Disadvantages: initialized when the class is loaded, wasting memory.

code show as below:

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

2, Example lazy single

It features of the model: does not generate a single case when the class is loaded only when the first call getlnstance method to create the singleton.

是否 Lazy 初始化:是
是否多线程安全:是
实现难度:复杂
描述:这种方式采用双重加锁机制,安全且在多线程情况下能保持高性能。
getInstance() 的性能对应用程序很关键。

代码如下:

public class Singleton {
    private volatile static Singleton instance = null;////保证 instance 在所有线程中同步

    private Singleton() {//private 避免类在外部被实例化
    }

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {//使用synchronized关键字来同步获取实例,保证单例的唯一性
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

从上述代码可以看到,双重加锁中有两次判断。第一次检查是避免不必要的上锁。相对加锁的懒汉模式而言,第二次检查是,在给当前线程加锁后,例行检查更加安全。

3、枚举

默认枚举实例的创建是线程安全的,并且在任何情况下都是单例。实际上
枚举类隐藏了私有的构造器。
枚举类的域 是相应类型的一个实例对象
是否 Lazy 初始化:否
是否多线程安全:是
实现难度:简单

描述:这种实现方式还没有被广泛采用,但这是实现单例模式的最佳方法。它更简洁,自动支持序列化机制,绝对防止多次实例化。这种方式是 Effective Java 作者 Josh Bloch 提倡的方式,它不仅能避免多线程同步问题,而且还自动支持序列化机制,防止反序列化重新创建新的对象,绝对防止多次实例化。不过,由于 JDK1.5 之后才加入 enum 特性,用这种方式写不免让人感觉生疏,在实际工作中,也很少用。不能通过 reflection attack 来调用私有构造方法。

代码如下:

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

图片显示

下面的是我的公众号二维码图片,欢迎关注。

图注:程序员Hotel
图注:程序员Hotel

Guess you like

Origin www.cnblogs.com/xiexinxin/p/12044229.html