- 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.");
}
}
推薦閱讀
- Spring 5.0 Microservices(Second Edition)
- PWA入門與實踐
- The Android Game Developer's Handbook
- SQL Server 2016從入門到精通(視頻教學超值版)
- Learning Neo4j 3.x(Second Edition)
- Data Analysis with Stata
- Reactive Programming With Java 9
- Python Web數據分析可視化:基于Django框架的開發實戰
- Mastering Business Intelligence with MicroStrategy
- Hands-On Nuxt.js Web Development
- HTML5權威指南
- HTML+CSS+JavaScript編程入門指南(全2冊)
- Cocos2d-x by Example:Beginner's Guide(Second Edition)
- Julia數據科學應用
- 人人都能開發RPA機器人:UiPath從入門到實戰