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();
}
}