恶汉式
public class Monitor1 { private Monitor1 (){} //创建Monitor1的无参构造器 private static Monitor1 monitor=new Monitor1(); //先创建Monitor1对象 //待有需求的时候再调用 public static Monitor1 getMonitor(){ return monitor; } }
懒汉式
public class Single { private static volatile Single instance; private Single() {} public static Single getInstance() { if (instance == null) { synchronized (Single.class) { if (instance == null) { instance = new Single(); } } } return instance; } }
来源:https://www.cnblogs.com/jxxblogs/p/12176684.html