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

JAVA:单例模式

单例模式是设计模式之一

设计模式,就像古代打仗,我们都听过孙子兵法,把计谋概括下来弄成一种模式,形成一种套路。

软件开发中也有很多场景,多数类似的问题场景,解决方案就形成固定的模式,单例模式就是其中一种

单例模式就是保证在某个类中只存在唯一一份实例,不会创建出多个实例

单例模式实现方式有很多,最常见的就是饿汉与懒汉两种模式,区别就是创建实例的时机不同

饿汉模式

饿汉模式是在类加载的时候创建

class Singleton{private static Singleton singleton=new Singleton();private Singleton(){}public static Singleton GetInstance(){return singleton;}
}

写一个具体的示例来看:

public class ThreadDemo1 {public static void main(String[] args) throws InterruptedException {Thread[] thread=new Thread[10];for(int i=0;i<5;i++){thread[i]=new Thread(()->{System.out.println(Singleton.GetInstance());});thread[i].start();}for (int i = 0; i < 5; i++) {thread[i].join();}}
}
class Singleton{private static Singleton singleton=new Singleton();private Singleton(){}public static Singleton GetInstance(){return singleton;}
}

根据运行结果我们能看到不同线程都是同一个实例 

懒汉模式

懒汉模式是在第一次使用的时候创建实例

单线程

class Singleton{private static Singleton instance=null;private Singleton (){}public static Singleton GetInstance(){if(instance==null) instance=new Singleton();return instance;}
}

多线程

在多线程情况下再使用单线程的懒汉模式是可能会出现线程不安全的,如果多个线程同时调用GetInstance 方法,可能就会出现多个实例

例如:

public class ThreadDemo2 {public static void main(String[] args) throws InterruptedException {Thread[] thread=new Thread[40];for(int i=0;i<40;i++){thread[i]=new Thread(()->{System.out.println(Singleton.GetInstance());});thread[i].start();}for(int i=0;i<40;i++){thread[i].join();}}
}
class Singleton{private static Singleton instance=null;private Singleton (){}public static Singleton GetInstance(){if(instance==null) instance=new Singleton();return instance;}
}

 在GetInstance 方法中加一个 synchronized 就能解决问题

public class ThreadDemo2 {public static void main(String[] args) throws InterruptedException {Thread[] thread=new Thread[40];for(int i=0;i<40;i++){thread[i]=new Thread(()->{System.out.println(Singleton.GetInstance());});thread[i].start();}for(int i=0;i<40;i++){thread[i].join();}}
}
class Singleton{private static Singleton instance=null;private Singleton (){}public static synchronized Singleton GetInstance(){if(instance==null) instance=new Singleton();return instance;}
}
 改进

加 synchronized 关键字确实解决了出现多个实例的问题,但是加锁与解锁是开销比较大的事,这里出现的线程不安全只发生在第一次创建实例时,在经过第一次创建实例后,后面就不需要加锁了

因此我们可以再加一个 if 判断一下

在给 instance 变量加上 volatile 关键字避免出现内存可见性的线程不安全

public class ThreadDemo2 {public static void main(String[] args) throws InterruptedException {Thread[] thread=new Thread[40];for(int i=0;i<40;i++){thread[i]=new Thread(()->{System.out.println(Singleton.GetInstance());});thread[i].start();}for(int i=0;i<40;i++){thread[i].join();}}
}
class Singleton{private static volatile Singleton instance=null;private Singleton (){}public static Singleton GetInstance(){if(instance==null){synchronized (Singleton.class){if(instance==null) instance=new Singleton();}}return instance;}
}

在 thread[1] 刚判断是否为空以后,thread[2] 也调用此方法并执行完,再执行thread[1] 后续加锁操作,这样也会创建多个实例

http://www.xdnf.cn/news/167959.html

相关文章:

  • 【含文档+PPT+源码】Python爬虫人口老龄化大数据分析平台的设计与实现
  • Python爬虫(6)静态页面解析实战:BeautifulSoup与lxml(XPath)高效提取数据指南
  • Kafka批量消费部分处理成功时的手动提交方案
  • C# 类的基本概念(声明类)
  • 技术分享 | Oracle-RAC修改IP信息
  • Redis超详细入门教程(基础篇)
  • redis_Windows中安装redis
  • Spring_MVC 中的 JSON 数据处理与 REST 风格开发
  • qt.qpa.plugin: Could not find the Qt platform plugin “cocoa“ in “ “
  • 蓝桥杯 2. 确定字符串是否是另一个的排列
  • 详解最新链路追踪skywalking框架介绍、架构、环境本地部署配置、整合微服务springcloudalibaba 、日志收集、自定义链路追踪、告警等
  • 第十六届蓝桥杯大赛软件赛省赛 C/C++ 大学B组 [京津冀]
  • 基于强化学习的智能交通控制系统设计
  • Eigen矩阵操作类 (Map, Block, 视图类)
  • 【JavaScript】逻辑运算符--非布尔值的与或运算、赋值运算符
  • 4月26日随笔
  • springboot应用使用shell脚本打包成部署压缩包(支持xjar)
  • AI心理健康服务平台项目面试实战
  • 使用Xshell中自带的传输新建文件功能实现上传下载文件
  • 树相关处理
  • UniApp 的现状与 WASM 支持的迫切性
  • w308汽车销售系统的设计与实现
  • 腾讯CSIG一面
  • 05--Altium Designer(AD)的详细安装
  • SM30 权限检查
  • 高中数学联赛模拟试题精选第18套几何题
  • GPU加速-系统CUDA12.5-Windows10
  • cron定时任务
  • Linux | Mfgtools 修改单独只烧写 Uboot,内核,文件系统
  • 前端面试宝典---vue实现简化版