我知道Java中的双重锁定已被破坏,那么在Java中使单例线程安全的最佳方法是什么?我想到的第一件事是:
class Singleton{
private static Singleton instance;
private Singleton(){}
public static synchronized Singleton getInstance(){
if(instance == null) instance = new Singleton();
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
这有用吗?如果是这样,它是最好的方式(我想这取决于具体情况,所以说明特定技术最好的时候会有用)