单例设计-静态内部类

public class InnerclassSingleton {
private InnerclassSingleton() {};

private static class Singleton {
private static final InnerclassSingleton i = new InnerclassSingleton();
}

public static InnerclassSingleton getInstance() {
return Singleton.i;
}
}

package com.example.SpringBootTest1.shejimoshi.singleton;
public class Test {
public static void main(String[] args) {
new Thread(() -> {
InnerclassSingleton instance1 = InnerclassSingleton.getInstance();
// com.example.SpringBootTest1.shejimoshi.singleton.InnerclassSingleton@6cb47364
System.out.println(instance1);
}).start();

new Thread(() -> {
InnerclassSingleton instance2 = InnerclassSingleton.getInstance();
// com.example.SpringBootTest1.shejimoshi.singleton.InnerclassSingleton@6cb47364
System.out.println(instance2);
}).start();
}
}

posted @ 2022-01-20 16:43  剑阁丶神灯  阅读(27)  评论(0编辑  收藏  举报