public class Singleton2 { //私有的构造方法 private Singleton2() { } //对外提供静态方法 //增加synchronized,保证线程安全性 public synchronized static Singleton2 getInstance() { if (instance == null) { instance = new Singleton2(); } return instance; } //静态实例 private static Singleton2 instance = new Singleton2(); }