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

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.");
}
}
主站蜘蛛池模板: 牡丹江市| 吉林省| 龙海市| 和政县| 鹿泉市| 阿勒泰市| 达州市| 许昌市| 宜兴市| 静海县| 宝丰县| 恭城| 十堰市| 仁怀市| 兴安盟| 延津县| 边坝县| 敦化市| 北海市| 中江县| 奎屯市| 昌黎县| 饶阳县| 平舆县| 自治县| 大荔县| 华池县| 闻喜县| 韶关市| 天柱县| 岐山县| 梅州市| 广汉市| 星子县| 鹤山市| 滨海县| 青浦区| 营口市| 长海县| 横山县| 嘉善县|