/* 两种单例模式的演示 */ //饿汉式 class Signal { private Signal(){} private Signal s = new Signal(); public static Signal getInstance() { return s; } } //懒汉式 class Signal { private Signal(){} private Signal s = null; public static Signal getInstance() { if(s == null) { synchronized(Signal.clss) { if(s == null) s = new Signal(); } } return s; } } //记录原则:定义单例,建议使用饿汉式。