- Design Patterns and Best Practices in Java
- Kamalmeet Singh Adrian Ianculescu LUCIAN PAUL TORJE
- 100字
- 2021-06-25 20:52:32
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.");
}
}
推薦閱讀
- Learning Data Mining with Python
- SQL for Data Analytics
- Vue.js 3.0源碼解析(微課視頻版)
- Android 7編程入門(mén)經(jīng)典:使用Android Studio 2(第4版)
- Windows Server 2012 Unified Remote Access Planning and Deployment
- 程序設(shè)計(jì)基礎(chǔ)教程:C語(yǔ)言
- 移動(dòng)界面(Web/App)Photoshop UI設(shè)計(jì)十全大補(bǔ)
- C語(yǔ)言程序設(shè)計(jì)
- Python深度學(xué)習(xí):模型、方法與實(shí)現(xiàn)
- 第一行代碼 C語(yǔ)言(視頻講解版)
- Swift 4從零到精通iOS開(kāi)發(fā)
- Vue.js應(yīng)用測(cè)試
- R語(yǔ)言:邁向大數(shù)據(jù)之路(加強(qiáng)版)
- 30天學(xué)通C#項(xiàng)目案例開(kāi)發(fā)
- Java高并發(fā)編程詳解:深入理解并發(fā)核心庫(kù)