提到单例模式,如下代码首当其冲:
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
学过多线程后,知道上述代码线程不安全,于是,代码修改为:
public static synchronized Singleton getInstance() {