Location>code7788 >text

Design Patterns Single Case Pattern

Popularity:573 ℃/2024-07-31 18:26:13

Double-checked locks

package .design_mode.singleton.double_check_lock; package .design_mode.singleton.double_check_lock; package .

package .design_mode.singleton.double_check_lock; import ;

design_mode.singleton.double_check_lock; import ;

/**
 * Double-checked locks
 */
public class SingletonTest {

    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                (().getName() + "Example object=" + ());
            }, "Thread" + i).start();
        }
    }
}

class Singleton {
    private static volatile Singleton instance = null;//volatile disable reordering

    private Singleton() {
    }

    @SneakyThrows
    public static Singleton getInstance() {
        if (instance == null) { // efficiency
            synchronized () {
                ("[debug]" + ().getName() + "Obtaining object lock");
                (2);
                if (instance == null) { // whether the object is null or not
                    /*
                    (1) Allocate stack space = " (2) Allocate heap memory = " (3) Stack pointer points to heap memory
                    Because of the reordering optimization
                    Thread A ① ③ stack pointer points to heap memory, the instance is not null after allocating memory.
                    Thread B enters the judgment instance!=null, and returns the incomplete instance created by thread A directly.
                    */
                    instance = new Singleton();
                }
                ("[debug]" + ().getName() + "Releasing object lock...") ;
            }
        }
        return instance; }
    }
}

static inner class (computing)

package .design_mode.singleton.static_inner_class;

import ;

import ;

/**
 * static inner class (computing)
 */
public class SingletonTest {

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                 a = new ();

                (());
            }).start();
        }
    }
}

class Singleton {

    private Singleton() {

    }

    static class SingletonHolder {
        //The virtual machine will ensure that a class <clinit>() Methods can be properly locked in a multi-threaded environment、synchronization。
        private static Singleton INSTANCE = new Singleton();
    }

    @SneakyThrows
    public static Singleton getInstance() {
        (3);
        return ;
    }
}

Hungry Man Style

package .design_mode.singleton.hungry_lock;

import ;

import ;

/*
 *Hungry Man Style
 * */
public class SingletonTest {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                (());
            }).start();
        }
    }
}

class Sinleton {

    private static final Sinleton instance = new Sinleton();

    private Sinleton() {
    }

    @SneakyThrows
    public static Sinleton getInstance() {
        (2);
        return instance;
    }
}