当前位置:网站首页>单例模式的懒汉模式跟恶汉模式的区别
单例模式的懒汉模式跟恶汉模式的区别
2022-07-01 16:26:00 【鸭蛋炒西红柿】
懒汉式是,只有用到这个用例的时候,再将他实例化,所以效率要高一些。
public class SingletonLH {
private static SingletonLH instance;
private SingletonLH(){};
public static SingletonLH getInstance(){
if (instance==null){
instance=new SingletonLH();
}
return instance;
}
public static void main(String[] args) {
SingletonLH instance = SingletonLH.getInstance();
System.out.println(instance);
}
}
饿汉式则是程序只要开始,就将他实例化,到用到他的时候就省去了再实例的时间,所以速度和反应快。这是这俩的区别
public class SingletonEH {
private static SingletonEH instance =new SingletonEH();
private SingletonEH(){};
public static SingletonEH getInstance(){
System.out.println("instance:"+instance);
System.out.println("加载饿汉式");
return instance;
}
public static void main(String[] args) {
SingletonEH instance = SingletonEH.getInstance();
System.out.println(instance);
}
}
两个最大的区别就是一个是开始的时候就实例化,一个是等到用的时候才实例化
边栏推荐
- How to use phpipam to manage IP addresses and subnets
- Red team Chapter 8: blind guess the difficult utilization process of the package to upload vulnerabilities
- 游戏行业安全选择游戏盾,效果怎么样?
- Template engine velocity Foundation
- Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out
- Principes et applications du système de base de données (006) - - compilation et installation de MySQL 5.7 (environnement Linux)
- Why is the pkg/errors tripartite library more recommended for go language error handling?
- SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
- Go 语言源码级调试器 Delve
- IM即時通訊開發實現心跳保活遇到的問題
猜你喜欢

How to maintain the laptop battery

Submission lottery - light application server essay solicitation activity (may) award announcement

Template engine velocity Foundation

PostgreSQL 存储结构浅析
![[nodemon] app crashed - waiting for file changes before starting...解决方法](/img/ee/9830afd86e092851a2a906cb994949.png)
[nodemon] app crashed - waiting for file changes before starting...解决方法

Im instant messaging develops a message delivery scheme for 10000 people

怎么用MySQL语言进行行列装置?

Hi Fun Summer, play SQL planner with starrocks!

Learn selenium to simulate mouse operation, and you can be lazy a little bit

sql刷题627. 变更性别
随机推荐
Problems encountered in IM instant messaging development to maintain heartbeat
Template engine velocity Foundation
Learn selenium to simulate mouse operation, and you can be lazy a little bit
Tutorial on principles and applications of database system (006) -- compiling and installing MySQL 5.7 (Linux Environment)
Flux d'entrées / sorties et opérations de fichiers en langage C
Origin2018 installation and use (sorting)
[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment
C语言输入/输出流和文件操作
【Kotlin】高阶函数介绍
红队第8篇:盲猜包体对上传漏洞的艰难利用过程
Talking from mlperf: how to lead the next wave of AI accelerator
vim用户自动命令示例
Basic use of MySQL
How to use phpipam to manage IP addresses and subnets
Research and investment strategy report of neutral protease industry in China (2022 Edition)
Endeavouros mobile hard disk installation
【flask入门系列】Cookie与Session
VMware virtual machine failed during startup: VMware Workstation is incompatible with hyper-v
Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
EndeavourOS移动硬盘安装