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

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.");
}
}
主站蜘蛛池模板: 三亚市| 即墨市| 仪征市| 望江县| 和田市| 义乌市| 福泉市| 涟源市| 宣化县| 长春市| 炎陵县| 扶风县| 卢龙县| 黎川县| 遂昌县| 新蔡县| 老河口市| 赣榆县| 宁夏| 武陟县| 安龙县| 凌源市| 兴和县| 宜兰市| 麻栗坡县| 万载县| 固镇县| 砚山县| 凭祥市| 连江县| 二手房| 蓝田县| 师宗县| 福清市| 新龙县| 南开区| 大方县| 彰武县| 明溪县| 长汀县| 广南县|