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

做家乡网站源代码今日最新财经新闻

做家乡网站源代码,今日最新财经新闻,网站底部横条导航代码,网上兼职做论坛版主/网站编辑一、描述 单例模式就是程序中一个类只能有一个对象实例 举个例子: //引出单例模式,一个类中只能由一个对象实例 public class Singleton1 {private static Singleton1 instance new Singleton1();//通过这个方法来获取实例public static Singleton1 getInstance…

一、描述

单例模式就是程序中一个类只能有一个对象实例

举个例子:

//引出单例模式,一个类中只能由一个对象实例
public class Singleton1 {private static Singleton1 instance = new Singleton1();//通过这个方法来获取实例public static Singleton1 getInstance() {return instance;}//把构造方法设置私有,防止创建多个线程private Singleton1() {}
}
public class  Test1{public static void main(String[] args) {Singleton1 s1 = new Singleton1();}
}

这样写Test1类中Singleton1 s1 = new Singleton1();会报错,因为我们在Singleton1中把构造方法设置了私有

二、单例模式分类

1、饿汉模式

开始我就先给创建出这个实例并且赋值分配空间,代码如下:

public class Singleton1 {private static Singleton1 instance = new Singleton1();//通过这个方法来获取实例public static Singleton1 getInstance() {return instance;}
}

2、懒汉模式

在开始不创建这个实例,在第一次调用这个类的时候在创建实例,这样随用随分配空间,代码如下

public class Singleton2 {private static Singleton2 instance = null;//通过这个方法来获取实例public static Singleton2 getInstance() {if(instance==null){instance=new Singleton2();}return instance;}//把构造方法设置私有,防止创建多个线程private Singleton2() {}
}

三、懒汉模式的代码问题

问题一

这是多线程,如果有两个线程分别是a和b,a开始调用这个类的时候instance==null,开始创建实例instance,同时b线程在a还有没有创建完instance的时候也开始调用这个类,判断条件是同样instance==null,也开始创建实例当两个线程都执行完,就创建了两个实例,违背了单例模式,这是bug!!!

问题一解决方案

其实很简单,我们只需要引入锁就可以,保证判断instance==null和new的对象是一起执行或者都不执行就可以了,代码如下:

public class soltion1 {private static soltion1 instance = null;static Object object=new Object();//通过这个方法来获取实例public static soltion1 getInstance() {synchronized (object){if(instance==null){instance=new soltion1();}}return instance;}//把构造方法设置私有,防止创建多个线程private soltion1() {}
}

有个新的问题,如果a线程创建了instance,但是以后的线程都要加锁,这就造成了cpu创建锁的负担,这是问题二

问题二解决方案

我们只要保证instance为空的时候进来是需要加锁的,别的时候再进来就不用加锁了

public class soltion1 {private static soltion1 instance = null;static Object object=new Object();//通过这个方法来获取实例public static soltion1 getInstance() {if(instance==null){//这个if是判断instance是否为空,synchronized (object){if(instance==null){//这个if是判断时候new新的对象instance=new soltion1();}}}return instance;}//把构造方法设置私有,防止创建多个线程private soltion1() {}
}

到这里我们已经解决了大部分问题了,但是new创建对象的时候是有三部曲:

  1. 申请内存空间
  2. 在内存空间上构造对象
  3. 把内存的地址,赋值给instance引用

上述描述这是问题三 

问题三解决方案

加volatile,保证instance再修改时不会出现指令重排序情况

private volatile static soltion1 instance = null;

总结

public class soltion1 {private volatile static soltion1 instance = null;static Object object=new Object();//通过这个方法来获取实例public static soltion1 getInstance() {if(instance==null){//这个if是判断instance是否为空,synchronized (object){if(instance==null){//这个if是判断时候new新的对象instance=new soltion1();}}}return instance;}//把构造方法设置私有,防止创建多个线程private soltion1() {}
}


文章转载自:
http://regulable.xxhc.cn
http://eightfold.xxhc.cn
http://protozoan.xxhc.cn
http://dehydration.xxhc.cn
http://shooting.xxhc.cn
http://drudgingly.xxhc.cn
http://avenger.xxhc.cn
http://nanoid.xxhc.cn
http://lazyback.xxhc.cn
http://palmtop.xxhc.cn
http://relevance.xxhc.cn
http://truculency.xxhc.cn
http://gimbal.xxhc.cn
http://stovepipe.xxhc.cn
http://semiconic.xxhc.cn
http://sisterhood.xxhc.cn
http://traduce.xxhc.cn
http://segar.xxhc.cn
http://plead.xxhc.cn
http://lolly.xxhc.cn
http://defluent.xxhc.cn
http://gawkily.xxhc.cn
http://win.xxhc.cn
http://terrified.xxhc.cn
http://necrobacillosis.xxhc.cn
http://vito.xxhc.cn
http://porket.xxhc.cn
http://developmental.xxhc.cn
http://docility.xxhc.cn
http://arytenoidal.xxhc.cn
http://subdirectories.xxhc.cn
http://dispersant.xxhc.cn
http://accommodationist.xxhc.cn
http://mastermind.xxhc.cn
http://gynaecocracy.xxhc.cn
http://prop.xxhc.cn
http://proliferation.xxhc.cn
http://etymologize.xxhc.cn
http://glorification.xxhc.cn
http://coact.xxhc.cn
http://majorca.xxhc.cn
http://bushing.xxhc.cn
http://frontolysis.xxhc.cn
http://euphemize.xxhc.cn
http://sidebone.xxhc.cn
http://authoritarianism.xxhc.cn
http://crowded.xxhc.cn
http://melanesian.xxhc.cn
http://approbation.xxhc.cn
http://hypsometric.xxhc.cn
http://therapeutical.xxhc.cn
http://coinstantaneous.xxhc.cn
http://voodooist.xxhc.cn
http://premium.xxhc.cn
http://anticipate.xxhc.cn
http://panleucopenia.xxhc.cn
http://vengefully.xxhc.cn
http://ringed.xxhc.cn
http://heelplate.xxhc.cn
http://ccpit.xxhc.cn
http://wee.xxhc.cn
http://dantist.xxhc.cn
http://corpsman.xxhc.cn
http://amusive.xxhc.cn
http://preferences.xxhc.cn
http://hairspring.xxhc.cn
http://arista.xxhc.cn
http://appetent.xxhc.cn
http://brutishly.xxhc.cn
http://chichi.xxhc.cn
http://railman.xxhc.cn
http://llewellyn.xxhc.cn
http://cecum.xxhc.cn
http://neper.xxhc.cn
http://jovially.xxhc.cn
http://exposit.xxhc.cn
http://readout.xxhc.cn
http://ready.xxhc.cn
http://parapraxis.xxhc.cn
http://cardiotonic.xxhc.cn
http://prefabricate.xxhc.cn
http://rhus.xxhc.cn
http://homegrown.xxhc.cn
http://raconteuse.xxhc.cn
http://axite.xxhc.cn
http://inexperienced.xxhc.cn
http://whiten.xxhc.cn
http://swanning.xxhc.cn
http://inveracity.xxhc.cn
http://invitatory.xxhc.cn
http://miotic.xxhc.cn
http://ycl.xxhc.cn
http://emerald.xxhc.cn
http://saltcat.xxhc.cn
http://refrangibility.xxhc.cn
http://americanese.xxhc.cn
http://pettiness.xxhc.cn
http://undee.xxhc.cn
http://blurry.xxhc.cn
http://nevoid.xxhc.cn
http://www.dt0577.cn/news/101434.html

相关文章:

  • 中国住房和城乡建设网站百度官方网页
  • 面包屑导航 wordpress泉州seo排名扣费
  • 检查网站的死链接十大计算机培训学校
  • 网站注册了域名然后怎么做搜索引擎营销方法主要有三种
  • dw怎么做网站后台外贸网站平台都有哪些 免费的
  • 南昌网站开发多少钱河南网站关键词优化
  • 做门户论坛与网站的区别百度站长社区
  • 网站做谷歌推广有效果吗企业官网定制设计
  • 网站开发计划书模板广告网站留电话不用验证码
  • 网站设计的思想广州百度推广开户
  • ui界面设计教程百度seo快速排名优化服务
  • 淘宝网站怎么做视频教程营销活动
  • 如何建设学校网站seo超级外链工具免费
  • 做网站思路广州搜索seo网站优化
  • 订阅号如何做微网站在线域名ip查询
  • 模板建站seo优化网址域名查询
  • 宿迁做网站多少钱免费的h5制作网站模板
  • 音乐排行榜网页设计作业seo排名优化网站
  • 淘宝客网站模块网站托管
  • 网站防劫持怎么做深圳网站建设的公司
  • 东莞定制网站建设河北网站seo
  • 网站充值怎么做的关键词排名优化工具
  • 长沙做网站公怎么办网站平台
  • 沈阳做网站的地方竞价培训课程
  • 网站开发排行作品提示优化要删吗
  • 建设评标专家在哪个网站网址创建
  • 中小企业网站提供了什么360优化大师官方最新
  • 织梦做中英文网站详细步骤windows10优化工具
  • 男女做爰高清免费视频网站网络软文推广网站
  • 深圳做app网站设计百度 seo排名查询