官术网_书友最值得收藏!

Lock-free thread-safe singleton

One of the best implementations of the singleton pattern in Java relies on the fact that a class is loaded a single time. By instantiating the static member directly when declared, we make sure that we have a single instance of the class. This implementation avoids locking mechanisms and additional checking to see whether the instance has already been created:

public class LockFreeSingleton
{
private static final LockFreeSingleton instance = new
LockFreeSingleton();
private LockFreeSingleton()
{
System.out.println("Singleton is Instantiated.");
}
public static synchronized LockFreeSingleton getInstance()
{
return instance;
}
public void doSomething()
{
System.out.println("Something is Done.");
}
}
主站蜘蛛池模板: 松江区| 温泉县| 嘉兴市| 沾益县| 固镇县| 广安市| 汉阴县| 措美县| 峨山| 科尔| 聂荣县| 商洛市| 湖北省| 鄄城县| 泰宁县| 长春市| 绥中县| 财经| 黔西| 黔西| 洛宁县| 含山县| 柳林县| 嘉峪关市| 锦屏县| 吴川市| 诸暨市| 通榆县| 伊吾县| 旬阳县| 勐海县| 孙吴县| 淮阳县| 榆林市| 大冶市| 色达县| 铅山县| 富民县| 屯昌县| 维西| 奈曼旗|