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

做公司网站成本百度电话

做公司网站成本,百度电话,天津网站建设制作系统,大连工业大学本科招生信息网1. 概念 保证一个类只有一个实例并为该实例提供一个全局唯一的访问节点 2. 懒汉式-方式一 2.1 代码示例(方式一) 示例 public class Singleton03 {/*** 构造器私有化*/private Singleton03() {}/*** 成员变量*/private static Singleton03 INSTANCE;…

1. 概念

  • 保证一个类只有一个实例
  • 并为该实例提供一个全局唯一的访问节点

2. 懒汉式-方式一

2.1 代码示例(方式一)

示例
public class Singleton03 {/*** 构造器私有化*/private Singleton03() {}/*** 成员变量*/private static Singleton03 INSTANCE;/*** 对外提供公有的静态方法*/public static Singleton03 getInstance() {// 用到才加载if (INSTANCE == null) {INSTANCE = new Singleton03();}return INSTANCE;}
}
public class SingletonTest03 {public static void main(String[] args) {Singleton03 instance = Singleton03.getInstance();Singleton03 instance1 = Singleton03.getInstance();System.out.println(instance == instance1);System.out.println("instance.hashCode= " + instance.hashCode());System.out.println("instance1.hashCode= " + instance1.hashCode());}
}

2.2 优缺点(方式一)

  1. 起到了Lazy Loading的效果,但是只能在单线程下使用。
  2. 如果在多线程下,一个线程进入了if(singleton==null)判断语句块,还未来得及
    往下执行,另一个线程也通过了这个判断语句,这时便会产生多个实例。所以
    在多线程环境下不可使用这种方式。

2.3 结论(方式一)

  • 线程不安全,在实际开发中,不要使用这种方式。

3. 懒汉式-方式二

3.1 代码示例(方式二)

示例
public class Singleton04 {/*** 构造器私有化*/private Singleton04() {}/*** 成员变量*/private static Singleton04 INSTANCE;/*** 对外提供公有的静态方法*/public static synchronized Singleton04 getInstance() {// 加入同步代码,解决线程不安全问题if (INSTANCE == null) {INSTANCE = new Singleton04();}return INSTANCE;}
}
public class SingletonTest04 {public static void main(String[] args) {Singleton04 instance = Singleton04.getInstance();Singleton04 instance1 = Singleton04.getInstance();System.out.println(instance == instance1);System.out.println("instance.hashCode= " + instance.hashCode());System.out.println("instance1.hashCode= " + instance1.hashCode());}
}

3.2 优缺点(方式二)

  1. 解决了线程不安全问题。
  2. 效率太低了,每个线程在想获得类的实例时候,执行getinstance()方法都要进行同步。而其实这个方法只执行一次实例化代码就够了,后面的想想获得该类实例,直接return就行了。方法进行同步效率太低。

3.3 结论(方式二)

  • 线程安全,但效率太低,在实际开发中,不推荐使用这种方式。

4. 懒汉式-方式三

4.1 代码示例(方式三)

示例
public class Singleton05 {private Singleton05() {}private static Singleton05 INSTANCE;public static Singleton05 getInstance() {if (INSTANCE == null) {synchronized (Singleton05.class) {INSTANCE = new Singleton05();}}return INSTANCE;}
}
public class SingletonTest05 {public static void main(String[] args) {Singleton05 instance = Singleton05.getInstance();Singleton05 instance1 = Singleton05.getInstance();System.out.println(instance == instance1);System.out.println("instance.hashCode= " + instance.hashCode());System.out.println("instance1.hashCode= " + instance1.hashCode());}
}

4.2 优缺点(方式三)

  1. 这种方式,本意是想对第四种实现方式的改进,因为前面同步方法效率太低,改为同步产生实例化的的代码块。
  2. 但是这种同步并不能起到线程同步的作用。跟方式一实现方式遇到的情形一致,假如一个线程进入了if(singleton==null)判断语句块,还未来来得及往下执行,另一个线程也通过了这个判断语句,这时便会产生多个实例。

4.3 结论(方式三)

  • 线程不安全,在实际开发中,不要使用这种方式

http://www.yidumall.com/news/29641.html

相关文章:

  • 成都网站建设招聘2023第二波疫情已经到来
  • 网站服务器 同步备份网络营销课程大概学什么内容
  • 政府网站集约化建设十分重要网站seo优化技巧
  • 淄博网站开发招聘如何在国外推广自己的网站
  • 太和县建设局网站在线收录
  • 做网站推广好做吗免费站推广网站2022
  • pc网站建设怎么做注册平台
  • 沈阳做机床的公司网站做网站seo怎么赚钱
  • 个人做网站名称可以随意更改吗广州关于进一步优化疫情防控措施
  • 沧州做网站公司最新今日头条
  • 广州做网站建设的公司哪家好1688精品货源网站入口
  • 深圳专业网站建设制作价格提升关键词
  • 新疆网站建设价格seo需要懂代码吗
  • b2c电商是什么意思seo网络推广经理招聘
  • 网站建设在医院的作用中国疫情今天最新消息
  • 刘家窑网站建设百度的搜索引擎优化
  • 网站视差怎么做国内免费建网站
  • 网站建设需要准备那些内容域名解析ip地址
  • 网站做预览文档域名查询网入口
  • 做网站的计划概要搜索引擎推广一般包括哪些
  • 新格建站定西seo排名
  • 成都网站建设外包太原seo网络优化招聘网
  • 动易手机网站模板电商产品推广方案
  • 网站建设操作系统阿里巴巴官网首页
  • 建网站北京企业做网上推广
  • 管理咨询公司注册资本专业的网站优化公司
  • 低价网站备案注册网址
  • 网站页面怎么做百度推广登录入口下载
  • 平邑网站开发seo教程自学网
  • 网站建设默认字体sns营销