Java implements creation mode-singleton mode (hungry man style, lazy man style, double check lock, singleton enumeration)

Java implements creation mode-singleton mode (hungry man style, lazy man style, double check lock, singleton enumeration)

Description of content

The problem solved by the singleton pattern is to ensure that there is only one instance of a class and provide a global access point to avoid conflicts and waste of resources between multiple objects. This pattern is often used to manage shared resources, such as database connection pools or thread pools.
This article implements four different types of singleton patterns: hungry, lazy, double-checked locks, and enumerations
To summarize the tests, two instances are created through the Main method to check whether they are the same object

public class testSingleton {
    
    
    public static void main(String[] args) {
    
    
        Singleton1 s1 = Singleton1.getInstance();
        Singleton1 s2 = Singleton1.getInstance();
        System.out.println(s1 == s2); // true

        Singleton2 s3 = Singleton2.getInstance();
        Singleton2 s4 = Singleton2.getInstance();
        System.out.println(s3 == s4); // true

        Singleton3 s5 = Singleton3.getInstance();
        Singleton3 s6 = Singleton3.getInstance();
        System.out.println(s5 == s6); // true

        Singleton4 s7 = Singleton4.INSTANCE;
        Singleton4 s8 = Singleton4.INSTANCE;
        System.out.println(s7 == s8); // true
    }
}

// 饿汉式单例模式
class Singleton1 {
    
    
    private static Singleton1 instance = new Singleton1();
    private Singleton1() {
    
    }
    public static Singleton1 getInstance() {
    
    
        return instance;
    }
}

// 懒汉式单例模式
class Singleton2 {
    
    
    private static Singleton2 instance;
    private Singleton2() {
    
    }
    public static synchronized Singleton2 getInstance() {
    
    
        if (instance == null) {
    
    
            instance = new Singleton2();
        }
        return instance;
    }
}

// 双重检查锁单例模式
class Singleton3 {
    
    
    private volatile static Singleton3 instance;
    private Singleton3() {
    
    }
    public static Singleton3 getInstance() {
    
    
        if (instance == null) {
    
    
            synchronized (Singleton3.class) {
    
    
                if (instance == null) {
    
    
                    instance = new Singleton3();
                }
            }
        }
        return instance;
    }
}

// 枚举单例模式
enum Singleton4 {
    
    
    INSTANCE;
}

hungry man mode

Hungry initialization creates an instance of the singleton class when the class is loaded. This ensures that the instance is always available and thread-safe, but can waste resources if the instance is not always needed.

// 饿汉式单例模式
class Singleton1 {
    
    
    private static Singleton1 instance = new Singleton1();
    private Singleton1() {
    
    }
    public static Singleton1 getInstance() {
    
    
        return instance;
    }
}

lazy mode

Lazy initialization creates instances of singleton classes only when needed. This saves resources, but is not thread-safe without synchronization.

// 懒汉式单例模式
class Singleton2 {
    
    
    private static Singleton2 instance;
    private Singleton2() {
    
    }
    public static synchronized Singleton2 getInstance() {
    
    
        if (instance == null) {
    
    
            instance = new Singleton2();
        }
        return instance;
    }
}

double check lock

Double-checked locking is a thread-safe version of lazy initialization that avoids the first synchronization after instance creation.

// 双重检查锁单例模式
class Singleton3 {
    
    
    private volatile static Singleton3 instance;
    private Singleton3() {
    
    }
    public static Singleton3 getInstance() {
    
    
        if (instance == null) {
    
    
            synchronized (Singleton3.class) {
    
    
                if (instance == null) {
    
    
                    instance = new Singleton3();
                }
            }
        }
        return instance;
    }
}

enum singleton

The enumeration singleton pattern is a simple thread-safe singleton pattern

// 枚举单例模式
enum Singleton4 {
    
    
    INSTANCE;
}

Guess you like

Origin blog.csdn.net/u010349629/article/details/129717447