单例设计模式(静态内部类方式:线程安全,且懒加载)

package com.jn.learning.javalearning.singleton;

/**********************************************************************************************
 * *
 * Written by TankPush (Jn)
 *
 * Description:
 * 静态内部类方式
 * 线程安全,且懒加载
 * *
 ***********************************************************************************************/
public class Singleton_InnerClass {

    private Singleton_InnerClass() {
    }

    private static class Builder {
        private static final Singleton_InnerClass instance = new Singleton_InnerClass();
    }

    public static Singleton_InnerClass getInstance() {
        return Builder.instance;
    }

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {

            new Thread(() -> {
                boolean isFlag = Singleton_InnerClass.getInstance() == Singleton_InnerClass.getInstance();
                System.out.println(isFlag);
            }).start();
            
        }

    }

}

你可能感兴趣的:(单例,设计模式,懒汉式)