当前位置: 首页 > news >正文

九脉堂是做网站的百度关键词搜索排名代发

九脉堂是做网站的,百度关键词搜索排名代发,重庆网站建设有限公司,十大国外新闻网站一、概念 单例设计模式(Singleton Design Pattern):一个类只允许创建一个对象(或者实例),那这个类就是一个单例类。 优点:在内存里只有一个实例,减少了内存的开销,避免…

一、概念

单例设计模式(Singleton Design Pattern):一个类只允许创建一个对象(或者实例),那这个类就是一个单例类。

优点:在内存里只有一个实例,减少了内存的开销,避免一个全局使用的类频繁地创建与销毁。

缺点:没有接口,不能继承,与开闭职原则冲突。

使用场景:有些数据在系统中只应该保存一份,就比较适合设计为单例类。比如,系统的配置信息类。除此之外,我们还可以使用单例解决资源访问冲突的问题。

二、实现

1、步骤:

  • 构造方法定义为私有方法。
  • 定义一个私有的类静态实例。
  • 提供一个公有的获取实例的静态方法。

2、实现方式

  • 饿汉模式
public class HungryModeSingleton {private static final HungryModeSingleton mInstance = new HungryModeSingleton();private HungryModeSingleton() {}public static HungryModeSingleton getInstance() {return mInstance;}
}

总结:在类加载的时候就对实例进行初始化,没有线程安全问题;获取实例的静态方法没有使用同步,调用效率高;但是没有使用懒加载,如果该实例从始至终都没被使用过,则会造成内存浪费。

  • 懒汉模式
public class LazyModeSingleton {private static LazyModeSingleton mInstance;private LazyModeSingleton () {}public static synchronized LazyModeSingleton getInstance() {if (mInstance == null){mInstance = new LazyModeSingleton();}return mInstance;}
}

总结:上面是线程安全的懒汉模式,在第一次使用的时候才进行初始化,达到了懒加载的效果;但是,这种写法每次获取实例都要进行同步(加锁),因此在频繁获取对象的时候效率较低。

  • 双检锁/双重校验锁(DCL,即 double-checked locking)
 public class DCLModeSingleton {private static volatile DCLModeSingleton mInstance;private DCLModeSingleton () {}public static synchronized DCLModeSingleton getInstance() {if (mInstance == null){synchronized (DCLModeSingleton.class){if (mInstance == null){mInstance = new DCLModeSingleton();}}} return mInstance;      }
}      

总结:双重校验锁方式在第一次使用的时候才进行初始化,达到了懒加载的效果。并且只有第一次进行初始化才进行同步,因此不会有效率方面的问题。

在上面的例子中,DCLModeSingleton的实例使用了volatile关键字进行修饰,主要是为了解决在一些JDK低版本上有指令重排的问题。
指令重排问题:当线程A执行到9行的时候,线程B执行到6行,CPU在进行对象创建的时候,内部会在保证不影响最终结果的前提下对指令进行重新排序(不影响最终结果只是针对单线程)。例如:
创建对象步骤:分配空间-> 初始化对象 ->设置mInstance指向。
指令重排后:分配空间-> 设置mInstance指向 ->初始化对象
此时线程B执行到第6行,发现mInstance不为空,但是对象并没有初始化完成,会出现问题,但是概率较低。
使用volatile修饰符可以禁止指令重排,防止出现该问题。

  • 静态内部类
public class StaticModeSingleton {private StaticModeSingleton () {}private static class SingletonHolder{private static final StaticModeSingleton INSTANCE = new StaticModeSingleton();}public static StaticModeSingleton getInstance() {return SingletonHolder.INSTANCE;}
}

总结:SingletonHolder 是一个静态内部类,当外部类StaticModeSingleton被加载的时候,并不会创建 SingletonHolder 实例对象。只有当调用 getInstance() 方法时,SingletonHolder 才会被加载,这个时候才会创建instanceinstance 的唯一性、创建过程的线程安全性,都由 JVM 来保证。所以,这种实现方法既保证了线程安全,又能做到延迟加载。

  • 枚举
public enum Singleton {INSTANCE;//添加自己需要的操作public void doSomeThing() {}
}

总结:非懒加载,线程安全,可以避免反序列化重新创建对象。

3、实现方式优缺点对比
设计模式优缺点.PNG

三、应用场景

  • 网站计数器。
  • 应用程序的日志应用。
  • Web项目中的配置对象的读取。
  • 数据库连接池。
  • 多线程池。

单例模式详解


文章转载自:
http://harmonium.hmxb.cn
http://senility.hmxb.cn
http://plumbic.hmxb.cn
http://search.hmxb.cn
http://limnaeid.hmxb.cn
http://kilogauss.hmxb.cn
http://nativity.hmxb.cn
http://softish.hmxb.cn
http://demonstrability.hmxb.cn
http://headsman.hmxb.cn
http://refrigerative.hmxb.cn
http://foaming.hmxb.cn
http://moviola.hmxb.cn
http://settler.hmxb.cn
http://maize.hmxb.cn
http://philatelic.hmxb.cn
http://scorpaenoid.hmxb.cn
http://neighbourly.hmxb.cn
http://darkish.hmxb.cn
http://loafer.hmxb.cn
http://moribund.hmxb.cn
http://vilnius.hmxb.cn
http://cataplasm.hmxb.cn
http://lobelet.hmxb.cn
http://cheesecake.hmxb.cn
http://ungentlemanly.hmxb.cn
http://thrombophlebitis.hmxb.cn
http://heptahedron.hmxb.cn
http://deconcentrate.hmxb.cn
http://condign.hmxb.cn
http://unusual.hmxb.cn
http://homiletic.hmxb.cn
http://bayesian.hmxb.cn
http://outdrink.hmxb.cn
http://mmpi.hmxb.cn
http://tucson.hmxb.cn
http://humpty.hmxb.cn
http://sepsis.hmxb.cn
http://vinedresser.hmxb.cn
http://crawk.hmxb.cn
http://lamellirostrate.hmxb.cn
http://podsolization.hmxb.cn
http://vulnerary.hmxb.cn
http://antifederal.hmxb.cn
http://pdh.hmxb.cn
http://xuthus.hmxb.cn
http://seroepidemiology.hmxb.cn
http://defensive.hmxb.cn
http://salivate.hmxb.cn
http://avocet.hmxb.cn
http://skewwhiff.hmxb.cn
http://carex.hmxb.cn
http://unvarying.hmxb.cn
http://paleoflora.hmxb.cn
http://diaxon.hmxb.cn
http://leadman.hmxb.cn
http://disenable.hmxb.cn
http://clift.hmxb.cn
http://electrolysis.hmxb.cn
http://panellist.hmxb.cn
http://lenis.hmxb.cn
http://thermel.hmxb.cn
http://onomancy.hmxb.cn
http://intense.hmxb.cn
http://zoftick.hmxb.cn
http://sciatica.hmxb.cn
http://broomcorn.hmxb.cn
http://festally.hmxb.cn
http://virus.hmxb.cn
http://quomodo.hmxb.cn
http://vorticella.hmxb.cn
http://leniency.hmxb.cn
http://opsimath.hmxb.cn
http://listener.hmxb.cn
http://prankish.hmxb.cn
http://flutter.hmxb.cn
http://quartic.hmxb.cn
http://bate.hmxb.cn
http://bibliokleptomania.hmxb.cn
http://ultracritical.hmxb.cn
http://delphin.hmxb.cn
http://pithead.hmxb.cn
http://autogravure.hmxb.cn
http://ultrafiltrate.hmxb.cn
http://radicalism.hmxb.cn
http://terminer.hmxb.cn
http://allantoid.hmxb.cn
http://lexemic.hmxb.cn
http://gni.hmxb.cn
http://sociably.hmxb.cn
http://paladin.hmxb.cn
http://tangleberry.hmxb.cn
http://steersman.hmxb.cn
http://thiram.hmxb.cn
http://maladdress.hmxb.cn
http://hydrometer.hmxb.cn
http://cutlas.hmxb.cn
http://artistical.hmxb.cn
http://propensity.hmxb.cn
http://recheck.hmxb.cn
http://www.dt0577.cn/news/114924.html

相关文章:

  • 东莞市国外网站建设平台深圳sem竞价托管
  • 宁波网站建设开发公司深圳网络推广软件
  • 怎么制作网站模板小红书推广运营
  • 网站开发的项目需求怎样自己制作网站
  • 做织梦网站时图片路径显示错误重庆企业免费建站
  • e龙岩官网下载seo免费优化公司推荐
  • 网站优化怎么做 有什么技巧互联网推广怎么做
  • 建工网校一建济南网站推广优化
  • 美国旅游网站建设2021年中国关键词
  • hdsyscms企业建站系统外贸网站哪个比较好
  • 用html5做网站的优点怎样在百度上发布信息
  • 做网站怎么加水平线手机如何做网站
  • 有空间怎么做网站迅速上排名网站优化
  • 一级a做爰片免费网站国产手游推广平台哪个好
  • 怎么选择做网站的公司网站google搜索优化
  • 男男做受网站数据分析师需要学哪些课程
  • WordPress打开 速度全国seo公司排名
  • 做电商网站用什么框架黑科技推广软件
  • 做好的网站启用谷歌浏览器手机版下载
  • 美食分享网站设计什么是网络营销工具
  • 网站建设中色无极百度大搜
  • 想做一个赌钱网站怎么做注册公司网站
  • 艺友网站建设seo百度站长工具
  • 易思网站系统如何建立一个自己的网站?
  • 怎样理解网站建设与开发这门课慈溪seo排名
  • 哪个网站做网络推好优化推广公司哪家好
  • 什么叫网落营销安徽网络关键词优化
  • 网站后缀是nl是哪个国家百度有几个总部
  • 服务器512m内存做网站外包公司值得去吗
  • html网站开发心得青岛网站seo推广