package com.kongjs.design_pattern.singleton_pattern;
public class App {
public static class App1 {
private static final App1 ourInstance = new App1();
private App1() {
System.out.println(getClass().getSimpleName());
}
public static App1 getInstance() {
return ourInstance;
}
}
public static class App2 {
private static App2 ourInstance = null;
private App2() {
System.out.println(getClass().getSimpleName());
}
public static App2 getInstance() {
if (ourInstance == null) {
ourInstance = new App2();
}
return ourInstance;
}
}
public static class App3 {
private static App3 ourInstance = null;
private App3() {
System.out.println(getClass().getSimpleName());
}
synchronized public static App3 getInstance() {
if (ourInstance == null) {
ourInstance = new App3();
}
return ourInstance;
}
}
}
public class Main {
public static void main(String[] args) {
Runnable r = ()->{
App.App1 app1 = App.App1.getInstance();
App.App2 app2 = App.App2.getInstance();
App.App3 app3 = App.App3.getInstance();
};
for (int i = 0; i <9 ; i++) {
new Thread(r).start();
}
}
}