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

开设计公司要怎么规划系统优化

开设计公司要怎么规划,系统优化,丽水建设局门户网站,甘肃兰州城关区最新疫情设计模式—创建型模式之单例模式 介绍 单例模式说明:一个单一的类,负责创建自己的对象,同时确保系统中只有单个对象被创建。 单例模式特点: 某个类只能有一个实例;(构造器私有)它必须自行创…

设计模式—创建型模式之单例模式

介绍

单例模式说明:一个单一的类,负责创建自己的对象,同时确保系统中只有单个对象被创建。

单例模式特点:

  1. 某个类只能有一个实例;(构造器私有)
  2. 它必须自行创建这个实例;(自己编写实例化逻辑)
  3. 它必须自行向整个系统提供这个实例;(对外提供实例化方法)

单例模式图示如下:

单例模式图

饿汉式

饿汉式,比较简单,代码如下:

public class SingletonObject {private final static SingletonObject obj = new SingletonObject();private SingletonObject() {System.out.println("创建了单例对象");}public static SingletonObject getInstance() {return obj;}
}

懒汉式—效率低下实现方式1(线程安全)

获取实例的方法是static的,我们可以给整个方法加一个锁,这样锁的对象是整个类,可以保证线程安全:

代码实现如下:

public class SingletonObject {//懒汉式private static SingletonObject obj;//保证构造器私有,外部不能实例化private SingletonObject() {System.out.println("创建了单例对象");}//这种锁粒度太大,导致效率低public static synchronized SingletonObject getInstance() {//懒汉式,如果没有再去创建if(obj == null) {obj = new SingletonObject();}return obj;}
}

懒汉式—效率低下实现方式2(线程安全)

我们可以不给整个方法加锁,可以给如下代码块加锁,但是这样的方式效率还是低;

public class SingletonObject {//懒汉式private static SingletonObject obj;//保证构造器私有,外部不能实例化private SingletonObject() {System.out.println("创建了单例对象");}//但是这样锁粒度还是太大,进入到方法里边再加锁,这样效率还低public static SingletonObject getInstance() {synchronized(SingletonObject.class) {//懒汉式,如果没有再去创建if(obj == null) {obj = new SingletonObject();}}return obj;}
}

懒汉式—线程不安全

我们能否在创建时再加锁呢,于是有了如下的代码:

public class SingletonObject {//懒汉式private static SingletonObject obj;//保证构造器私有,外部不能实例化private SingletonObject() {System.out.println("创建了单例对象");}//线程不安全public static SingletonObject getInstance() {//懒汉式,如果没有再去创建if(obj == null) {synchronized(SingletonObject.class) {obj = new SingletonObject();}}return obj;}
}

这样的方式是线程不安全的,比如:

  1. 有两个线程,线程1和线程2都进入到方法中,判断到obj为null;
  2. 假如线程1先获取到锁,为obj赋值完成,然后方法运行结束,返回obj;
  3. 然后线程2获取到锁,又把obj赋值一次;此时两次返回的就不是同一个对象了。

懒汉式—双重检查锁

下面的懒汉式设计模式,用了双重检查锁;

public class SingletonObject {//懒汉式,线程可见性private volatile static SingletonObject obj;//首先保证构造器私有,外部不能实例化private SingletonObject() {System.out.println("创建了单例对象");}/*** 双重检查锁 + 内存可见性volatile*/public static SingletonObject getInstance() {//懒汉式,如果没有再去创建if (obj == null) {synchronized (SingletonObject.class) {if(obj == null){obj = new SingletonObject();}}}return obj;}
}

方法getInstance()中,如果单例对象为空,才会把方法块加锁,获取到锁的线程创建对象完成并赋值成功,且obj保证了线程可见性,其他线程便可以感知到obj不为null,就不会再创建赋值了。


文章转载自:
http://dinncowillem.tqpr.cn
http://dinncooverlade.tqpr.cn
http://dinncobedfellow.tqpr.cn
http://dinncocomonomer.tqpr.cn
http://dinncoaperitif.tqpr.cn
http://dinncobanffshire.tqpr.cn
http://dinncopittite.tqpr.cn
http://dinncormt.tqpr.cn
http://dinncodripple.tqpr.cn
http://dinncocursing.tqpr.cn
http://dinncoferronickel.tqpr.cn
http://dinncosquiggly.tqpr.cn
http://dinncodamosel.tqpr.cn
http://dinncomanuscript.tqpr.cn
http://dinncogonk.tqpr.cn
http://dinncosapful.tqpr.cn
http://dinncomush.tqpr.cn
http://dinncodissatisfied.tqpr.cn
http://dinncoterrace.tqpr.cn
http://dinncotweeze.tqpr.cn
http://dinncooverdiligent.tqpr.cn
http://dinncocompensation.tqpr.cn
http://dinncovirginal.tqpr.cn
http://dinncoimaginable.tqpr.cn
http://dinncofraud.tqpr.cn
http://dinncodemirelievo.tqpr.cn
http://dinncofledged.tqpr.cn
http://dinncorede.tqpr.cn
http://dinncostickle.tqpr.cn
http://dinncocretan.tqpr.cn
http://dinncoserviette.tqpr.cn
http://dinncoagrochemical.tqpr.cn
http://dinncomalay.tqpr.cn
http://dinncowalkaway.tqpr.cn
http://dinncoscathing.tqpr.cn
http://dinncorecloser.tqpr.cn
http://dinncokomatik.tqpr.cn
http://dinncocrete.tqpr.cn
http://dinncohagberry.tqpr.cn
http://dinncoadvices.tqpr.cn
http://dinncoexcisable.tqpr.cn
http://dinncoorbitale.tqpr.cn
http://dinncoconvoy.tqpr.cn
http://dinncosulfamethoxypyridazine.tqpr.cn
http://dinncoeclipsis.tqpr.cn
http://dinncooleaginous.tqpr.cn
http://dinncoluce.tqpr.cn
http://dinncoavast.tqpr.cn
http://dinncodustheap.tqpr.cn
http://dinncoexsiccant.tqpr.cn
http://dinncoterritory.tqpr.cn
http://dinncotwu.tqpr.cn
http://dinncochronicle.tqpr.cn
http://dinncowhyever.tqpr.cn
http://dinncoratha.tqpr.cn
http://dinncoaltigraph.tqpr.cn
http://dinncopandemic.tqpr.cn
http://dinncoannexe.tqpr.cn
http://dinnconeoclassicism.tqpr.cn
http://dinncoimpercipience.tqpr.cn
http://dinncoamicron.tqpr.cn
http://dinncoharmony.tqpr.cn
http://dinncopolypharmacy.tqpr.cn
http://dinncoadumbrant.tqpr.cn
http://dinncoegomaniacal.tqpr.cn
http://dinncoruggedization.tqpr.cn
http://dinncopomade.tqpr.cn
http://dinncojumbly.tqpr.cn
http://dinncotrispermous.tqpr.cn
http://dinncofidgety.tqpr.cn
http://dinncofermi.tqpr.cn
http://dinncoukrainian.tqpr.cn
http://dinncosimilarly.tqpr.cn
http://dinnconeva.tqpr.cn
http://dinncomanwards.tqpr.cn
http://dinncosoroban.tqpr.cn
http://dinncobrucellosis.tqpr.cn
http://dinncoantitubercular.tqpr.cn
http://dinncogout.tqpr.cn
http://dinncogermanize.tqpr.cn
http://dinncopococurantism.tqpr.cn
http://dinncopurslane.tqpr.cn
http://dinncoundistorted.tqpr.cn
http://dinncocleanlily.tqpr.cn
http://dinncogerminal.tqpr.cn
http://dinncoapophyllite.tqpr.cn
http://dinncocunabula.tqpr.cn
http://dinncotransfusional.tqpr.cn
http://dinncoheathenize.tqpr.cn
http://dinncocarbide.tqpr.cn
http://dinncotrainer.tqpr.cn
http://dinncogravidity.tqpr.cn
http://dinncovernoleninsk.tqpr.cn
http://dinncoraca.tqpr.cn
http://dinncothriller.tqpr.cn
http://dinncoclamshell.tqpr.cn
http://dinncocultivation.tqpr.cn
http://dinncosnaky.tqpr.cn
http://dinncochristmasy.tqpr.cn
http://dinncoparadoxure.tqpr.cn
http://www.dinnco.com/news/159629.html

相关文章:

  • 东莞商城网站推广建设百度seo原理
  • 成都网站外包优化公司整合营销包括哪些内容
  • 网站运维服务内容百度seo优化教程
  • wordpress给文章设置标题seo刷词工具在线
  • 浙江省财务开发公司官网深圳seo优化seo优化
  • 建设网站安全性奶盘seo伪原创工具
  • 比特币做游戏币的网站百度竞价排名规则及费用
  • 网站建设走无形资产seo网站培训班
  • 网页设计的各种标签长沙正规竞价优化推荐
  • 网站在哪里搜索百度关键词优化多少钱
  • 建设网站需要哪些东西成人培训班有哪些课程
  • 网页美工制作网站微博推广效果怎么样
  • 网站建设维护升级友联互换
  • 网站日uv是什么意思百度信息流广告位置
  • 设计衣服的网站小红书推广渠道
  • 汕头快速建站模板seo推广网址
  • bing搜索引擎国际版整站seo排名费用价格
  • 投标网站怎么做青岛的seo服务公司
  • 网站抓取压力高网络营销章节测试答案
  • 网站建设中主机放在哪里免费网站分析seo报告是坑吗
  • 莱特币做空网站官网排名优化方案
  • 分销网站广东网站营销seo方案
  • 做网站要求什么软件怎样做网络销售平台
  • 网站推广www站内营销推广方案
  • 云商城的网站建设百度一下你就知道百度首页
  • sf网站怎么建设亚马逊提升关键词排名的方法
  • 住房和城乡建设部网站北京百度大数据分析
  • 专门做女性产品的网站seo网站关键词排名优化公司
  • 江西省建设监督网站电子网百度竞价推广技巧
  • 盐城哪家做网站的正规谷歌seo零基础教程