单例模式 Posted on 2021-02-14 12345678910111213141516171819202122232425262728293031323334353637383940/** * * @author devin * @date 2021-01-08 11:12 * @version 1.0.2*/public class SingleTest { private static volatile SingleTest instance; private SingleTest(){ System.out.println("create method"); } //在这里加同步太重 public static SingleTest getInstance(){ if(instance == null){ synchronized (SingleTest.class){ if(instance == null){ return instance = new SingleTest(); } } } return instance; } public static void main(String[] args) { for(int i = 0;i < 1000;i++){ new Thread(){ @Override public void run() { SingleTest.getInstance(); } }.start(); } }} Donate comment here Donate WeChat Pay