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

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.");
}
}
主站蜘蛛池模板: 东光县| 陇西县| 桐乡市| 高清| 任丘市| 黔东| 弥渡县| 盐边县| 绥江县| 古蔺县| 滨州市| 于都县| 中方县| 东平县| 汾阳市| 行唐县| 奉节县| 霞浦县| 乐陵市| 宝鸡市| 辽源市| 三穗县| 井冈山市| 宜良县| 呼图壁县| 武汉市| 大姚县| 临西县| 赣州市| 原阳县| 铜鼓县| 信宜市| 拜城县| 江川县| 扎囊县| 江津市| 武穴市| 龙山县| 齐河县| 文水县| 元谋县|