Singleton mode under thread safety

Keywords: back-end development, design patterns, Java.

Single-threaded singleton

Private construction method
Holds the static private properties of
your own class Static method to obtain an instance of your own class It
is not safe in multi-threaded scenarios
Insert picture description here

Thread-safe singleton under multithreading

Private construction method
Holds the private properties of
your own class Method to obtain an instance of your own class
synchronized synchronization, the key to thread safety
Insert picture description here

Absolutely thread-safe singleton (officially recommended)

Private construction method, enumeration is actually private by default
. Static method to obtain own class instance.
Internal enumeration class
Insert picture description here

There are many ways to implement the singleton mode, but there are three same characteristics:

The constructor is private to prevent external new objects from
containing attributes of their own types;
static methods for obtaining instances are provided externally;

scenes to be used:

Tool classes, such as tool methods for obtaining all cities;
classes that consume resources to create objects

Expand knowledge

Through reflection technology, other singleton modes other than enumeration can be destroyed.
Insert picture description here
The console result output is:
Insert picture description here

Analysis of the reasons for the absolute thread safety of enumerations:

The final decompiled source code of the enumeration type:

public final class T extends Enum
{
    
    
    private T(String s, int i)
    {
    
    
        super(s, i);
    }
    public static T[] values()
    {
    
    
        T at[];
        int i;
        T at1[];
        System.arraycopy(at = ENUM$VALUES, 0, at1 = new T[i = at.length], 0, i);
        return at1;
    }

    public static T valueOf(String s)
    {
    
    
        return (T)Enum.valueOf(demo/T, s);
    }

    public static final T SPRING;
    public static final T SUMMER;
    public static final T AUTUMN;
    public static final T WINTER;
    private static final T ENUM$VALUES[];
    static
    {
    
    
        SPRING = new T("SPRING", 0);
        SUMMER = new T("SUMMER", 1);
        AUTUMN = new T("AUTUMN", 2);
        WINTER = new T("WINTER", 3);
        ENUM$VALUES = (new T[] {
    
    
            SPRING, SUMMER, AUTUMN, WINTER
        });
    }
}

After decompiling the code, we can see that the public final class T extends Enum indicates that this class inherits the Enum class, and the final keyword tells us that this class cannot be inherited. When we use enmu to define an enumeration type, the compiler will automatically help us create a final type class inheriting the Enum class, so the enumeration type cannot be inherited. We see that there are several properties and methods in this class.

And all member variables and member methods are of static type, because static type properties will be initialized after the class is loaded. When a Java class is actually used for the first time, the static resources are initialized and the Java class is loaded. And the initialization process is thread-safe. Therefore, creating an enum type is thread-safe.

Guess you like

Origin blog.csdn.net/weixin_46011971/article/details/107051057