/**
* 饿汉式
*
*/
public class Singleton {
private static Singleton instance = new Singleton();
//私有的默认构造子,保证外界无法直接实例化
private Singleton() {}
//提供全局访问点获取唯一的实例
public static Singleton getInstance() {
return instance;
}
}
/**
* 懒汉式
*/
public class Singleton {
private static Singleton instance;
//私有的默认构造子,保证外界无法直接实例化
private Singleton() {}
public static synchronized Singleton getInstance() {
if(instance==null){
instance = new Singleton();
}
return instance;
}
}
/**
* 懒汉式(双重锁)
*/
public class Singleton {
//使用 volatile 保证可见性
private volatile static Singleton instance;
//私有的默认构造子,保证外界无法直接实例化
private Singleton() {}
public static Singleton getInstance() {
if(instance==null){
synchronized(Singleton.class){
if(instance==null){
instance = new Singleton();
}
}
}
return instance;
}
}
/**
* 内部类
*/
public class Singleton {
//私有的默认构造子,保证外界无法直接实例化
private Singleton() {}
/**
* 类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例
* 没有绑定关系,而且只有被调用到才会装载,从而实现了延迟加载
*/
private static class SingletonHolder{
/**
* 静态初始化器,由JVM来保证线程安全
*/
private static Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有