单例模式加锁优化问题
2016-07-21 16:39
253 查看
单例模式
单例模式特点:只有一个实例
私有构造函数
单例模式应用场景:
具有资源管理器的功能的应用,如打印机、线程池、缓存、显卡驱动等等
像我这初学的渣渣程序媛,在Android开发中用的最多的地方就是数据库DAO层中~~~~
单例模式类型:
饿汉式、懒汉式、登记式
饿汉模式
优点:一开始就创建实例,线程安全缺点:占内存,耗资源
public class Singleton { private static Singleton uniqueInstance = new Singleton(); //创建私有的构造方法 private Singleton() { ... } //创建公有静态实例 public static Singleton getInstance() { return uniqueInstance; }
懒汉模式
优点:调用实例静态方法时,没有创建实例,再创建;合理利用资源,减少资源浪费缺点:可能会引起线程冲突,不安全
public class Singleton { private static Singleton uniqueInstance = null; //创建私有的构造方法 private Singleton() { ... } //创建公有静态实例 public static Singleton getInstance() { if(uniqueInstance == null){//当没有实例的时候,再创建实例 uniqueInstance = new Singleton(); } return uniqueInstance; }
优化1:为了解决线程冲突,加锁。这样只有当一个线程释放锁后,下一个线程才能得到锁,进去。
public class Singleton { private static Singleton uniqueInstance = null; //创建私有的构造方法 private Singleton() { ... } //创建公有静态实例,注意在实例方法前加了synchronized public synchronized static Singleton getInstance() { if(uniqueInstance == null){//当没有实例的时候,再创建实例 uniqueInstance = new Singleton(); } return uniqueInstance; }
但是这样每个进来的线程都加锁后再判断实例是否已经存在,然而加锁的次数越多,代码执行性能越慢,所以为了减少不必要的加锁次数,进行再次优化。
优化2:减少加锁次数
public class Singleton { private static Singleton uniqueInstance = null; //创建私有的构造方法 private Singleton() { ... } //创建公有静态实例,注意在实例方法前加了synchronized public static Singleton getInstance() { if (uniqueInstance == null) { // 在判断实例是否存在的时候,再加锁 synchronized (Singleton.class) { if (uniqueInstance == null) { uniqueInstance = new Singleton(); } } } return uniqueInstance; }
二次优化后,当实例已经存在的时候就直接返回实例,提高了代码执行性能,且线程安全。
登记式
登记式单例模式用的不多,它主要是对一组单例模式进行的维护,主要是在数量上的扩展通过map我们把单例存进去,这样在调用时,先判断该单例是否已经创建,是的话直接返回,不是的话创建一个登记到map中,再返回。对于数量又分为固定数量和不固定数量的。下面采用的是不固定数量的方式,在getInstance方法中加上参数(string name)。然后通过子类继承,重写这个方法将name传进去。让我们看看代码吧。(引用http://blog.csdn.net/lanzhizhuxia/article/details/7922977)
public class Singleton3 { private static Map<String,Singleton3> map = new HashMap<String,Singleton3>(); static{ //类似饿汉模式,类加载就创建实例 Singleton3 single = new Singleton3(); map.put(single.getClass().getName(), single); } //protected类型的默认构造子 protected Singleton3(){} //静态工厂方法,返还此类惟一的实例 public static Singleton3 getInstance(String name) { if(name == null) { name = Singleton3.class.getName(); } if(map.get(name) == null) { try { map.put(name, (Singleton3) Class.forName(name).newInstance()); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } return map.get(name); } .... }
相关文章推荐
- openstack安装配置(三)
- 使用git往github上提交代码时出现[packet_write_wait connection to xx.xx.xx.xx Broken pipe]错误的解决办法
- CentOS 6.4 下安装vsftpd
- 阿里云日志服务日志写入华东2配置
- Mysql Create命令小结
- Android Dialog 的一些特性
- 【渗透测试学习平台】 web for pentester -3.XSS
- Android Studio新功能解析,你真的了解Instant Run吗?
- JAVA用画板JFrame画布Panel画笔Graphics画一个星星,可拖动-3
- 比较复杂的创建APD
- JS循环遍历JSON数组
- 2016.7.21 多校赛 1009 (水题)
- 3.2版本如何导入扩展类
- Android ListView介绍及优化方案
- Radiomics-从医学图像中获取更多信息
- 如何在多台web服务器上共享session?
- Hibernate中HQL函数汇总及其说明
- Camshift文献学习与研究
- The Text Splitting CodeForces 612A
- [DP46题] HDU 1864 最大报销额