当前位置:网站首页>单例模式的懒汉模式跟恶汉模式的区别
单例模式的懒汉模式跟恶汉模式的区别
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);
}
}
两个最大的区别就是一个是开始的时候就实例化,一个是等到用的时候才实例化
边栏推荐
- 复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)
- Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
- VMware 虚拟机启动时出现故障:VMware Workstation 与 Hyper-v 不兼容...
- 數據庫系統原理與應用教程(006)—— 編譯安裝 MySQL5.7(Linux 環境)
- 阿里云、追一科技抢滩对话式AI
- 游戏行业安全选择游戏盾,效果怎么样?
- Go 语言怎么优化重复的 if err != nil 样板代码?
- Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
- 数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
- sql刷题584. 寻找用户推荐人
猜你喜欢
Tutorial on the principle and application of database system (002) -- MySQL installation and configuration: MySQL software uninstallation (Windows Environment)
全面看待企业数字化转型的价值
C语言输入/输出流和文件操作
【SQL语句】请问这边为什么select出了两个上海,查询出了不同的count我想让他变成一个上海,count只显示一个总和
Bugku's file contains
Is the programmer's career really short?
Kali install Nessus
Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?
为国产数据库添砖加瓦,StoneDB 一体化实时 HTAP 数据库正式开源!
How to use F1 to F12 correctly on laptop keyboard
随机推荐
Dataframe gets the number of words in the string
Go 语言怎么使用对称加密?
Tutorial on principles and applications of database system (004) -- MySQL installation and configuration: resetting MySQL login password (Windows Environment)
sql刷题627. 变更性别
模板引擎Velocity 基礎
How long will it take to achieve digital immortality? Metacosmic holographic human avatar 8i
Tutorial on principles and applications of database system (006) -- compiling and installing MySQL 5.7 (Linux Environment)
Five years after graduation, I became a test development engineer with an annual salary of 30w+
Learn selenium to simulate mouse operation, and you can be lazy a little bit
Origin2018 installation and use (sorting)
Submission lottery - light application server essay solicitation activity (may) award announcement
How to use phpipam to manage IP addresses and subnets
FPN网络详解
Red team Chapter 8: blind guess the difficult utilization process of the package to upload vulnerabilities
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!
[kotlin] Introduction to higher-order functions
游戏行业安全选择游戏盾,效果怎么样?
Rhcsa Road