线程安全的单例模式
来源:互联网 发布:莫斯科地铁软件 编辑:程序博客网 时间:2024/04/30 22:06
- public
final class EagerSingleton - {
-
private static EagerSingleton newsingObj = EagerSingleton(); -
-
private EagerSingleton() -
} -
-
public static EagerSingleton getSingleInstance() -
singObj; -
} - }
- public
final class LazySingleton - {
-
private static LazySingleton null;singObj = -
-
private LazySingleton() -
} -
-
public static LazySingleton getSingleInstance() -
if(null == singObj ) singObj =new LazySingleton(); -
return singObj; -
} - }
- public
final class ThreadSafeSingleton - {
-
private static ThreadSafeSingleton null;singObj = -
-
private ThreadSafeSingleton() -
} -
-
public staticSynchronized ThreadSafeSingleton getSingleInstance() -
if(null == singObj ) singObj= new ThreadSafeSingleton(); -
singObj; -
} - }
- public
final class DoubleCheckedSingleton - {
-
private static DoubleCheckedSingletonsi null;ngObj = -
-
private DoubleCheckedSingleton() -
} -
-
public static DoubleCheckedSingleton getSingleInstance() -
if(null == singObj ){ -
-
if(null == singObj) -
DoubleCheckedSingleton(); -
-
} -
return singObj; -
} - }
- public
class Singleton - {
-
private static class SingletonHolder -
{ -
public final static Singleton newinstance = Singleton(); -
} -
-
public static Singleton getInstance() -
{ -
return SingletonHolder.instance; -
} - }
至此,本文完。提供一些链接For your reference:
Double-Checked Lock:http://en.wikipedia.org/wiki/Double-checked_locking
Initialzation on Demand Holder: http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom
0 0
- 线程安全的单例模式
- 线程安全的单例模式
- 线程安全的单例模式
- 线程安全的单例模式
- Volatile 线程安全的单例模式
- 线程安全的单例模式
- 基于线程安全的单例模式
- 线程安全的单例模式
- 单例模式的线程安全
- 线程安全的单例模式
- 线程安全的单例模式
- 线程安全的单例模式
- 线程安全的单例模式
- 线程安全的单例模式
- 线程安全的单例模式 [老生常谈]
- java线程安全的单例模式
- 线程安全的单例模式
- + 线程安全的单例模式
- 在ubuntu中如何重置mysql服务器的root密码
- tableview索引添加一个显示点击或滑动到哪个字母的控件
- Java 枚举项数量限定为64个以内
- onvif vs psia
- 【干货分享】Node.js 中文资料导航
- 线程安全的单例模式
- 【项目1】实现复数类中的运算符重载--用类的友元函数,完成运算符的重载;
- oracle数据库连接失败之ORA-01034: ORACLE notavailable
- 第四章作业
- sqlite工具类
- iOS7修改状态栏文字颜色
- 34去房间哦
- LeetCode Reverse Polish Notation求逆波兰表达式值
- Mac Windows 电脑访问 共享文件