public class Singleton1 {
private Singleton1() {
}
// 在自己内部定义自己一个实例.
// 注意这是private 只供内部调用
private static Singleton1 instance = new Singleton1();
/** *//**
* 这里提供了一个供外部访问本class的静态方法,可以直接访问
* @return
*/
public static Singleton1 getInstance() {
return instance;
}
}
public class Singleton2 {
private static Singleton2 instance = null;
/** *//**
* 这个方法比上面有所改进,不用每次都进行生成对象,只是第一次
* 使用时生成实例,提高了效率!
* @return
*/
public static Singleton2 getInstance() {
if (instance == null)
instance = new Singleton2();
return instance;
}
}
public class Singleton3 {
private static Singleton3 instance = null;
/** *//**
* 这个方法比上面有所改进,不用每次都进行生成对象,只是第一次
* 使用时生成实例,提高了效率!
* 为了多线程不出错,加入了同步标志
* @return
*/
public static synchronized Singleton3 getInstance() {
if (instance == null)
instance = new Singleton3();
return instance;
}
}
public class Singleton4 {
private static volatile Singleton4 instance;
/** *//**
* 双重加锁实现多线程运用和性能优化
* @return
*/
public static Singleton4 getInstance()
{
if (instance == null)
{
synchronized(Singleton4.class) { //1
if (instance == null) //2
instance = new Singleton4(); //3
}
}
return instance;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有