当前位置:网站首页>懒汉式单例模式
懒汉式单例模式
2022-06-11 00:27:00 【haohulala】
今天面试被问了懒汉式单例模式,没有答上来,现将答案记录下来
public class Solutionaaa {
private volatile static Solutionaaa solutionaaa = null; // 1
private Solutionaaa(){
}
public static Solutionaaa getSolutionaaa(){
if(solutionaaa == null){ // 2
synchronized (Solutionaaa.class){ // 3
if(solutionaaa == null){ // 4
solutionaaa = new Solutionaaa(); // 5
}
}
}
return solutionaaa;
}
}以上便是懒汉式单例模式的写法,现在对于代码做一个解析
1. 使用volatile是为了防止指令重排
2. 是为了加快多线程模式下的运行速率,比如已经创建了一个实例,那么就不需要去竞争锁了
3+4. 是为了防止多线程模式下重复创建单例。有可能线程A竞争到了锁,然后创建了实例,退出了锁,此时线程B也进到临界区,此时如果没有if判断是否为null,那么就会重复创建实例
5. 创建单例
边栏推荐
- 2.1、ROS+PX4仿真---定点飞行控制
- Sealem Finance打造Web3去中心化金融平台基础设施
- Introduction to the policy support of Beijing China Patent Award, with a subsidy of 1million yuan
- Current limiting and download interface request number control
- 北京密云区高新技术企业培育支持标准,补贴10万
- SQL question brushing and sorting in leetcode of partial deduction
- HandlerMethodArgumentResolver(参数解析器)的作用+使用小案例
- One way linked list to realize student information management
- 中国专利奖奖金多少,补贴100万
- Beijing Pinggu District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
猜你喜欢

Current limiting and download interface request number control

Brief description of custom annotations

如何使用自定义注解进行参数校验

Once you know these treasure websites, you can't live without them!!!
![[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields](/img/c6/f1cec6de62e85de446dba7ea8675f0.jpg)
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields

Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack
![[Li mu] how to read papers [intensive reading of papers]](/img/41/7e1ff1db2f7a848c8702c186c79fe5.jpg)
[Li mu] how to read papers [intensive reading of papers]

Implementing MySQL fuzzy search with node and express

OCR文字识别经典论文详解

A tutorial on building a website from scratch with complete steps (7000 words and 102 screenshots for everyone to understand, with source code attached)
随机推荐
Understanding of multithreading
CSRF attack
From "0" to "tens of millions" concurrency, 14 technological innovations of Alibaba distributed architecture
Web3 ecological decentralized financial platform sealem Finance
Yunna provincial administrative unit fixed assets management system
SAS principal component analysis (finding correlation matrix, eigenvalue, unit eigenvector, principal component expression, contribution rate and cumulative contribution rate, and data interpretation)
深圳市南山区专精特新企业申报条件,补贴10-50万
Clean up the broken artifacts data (.lastUpdated files) and reload the project. Problem resolution
Inventory management and strategy mode
Introduction and creation of Huffman tree
PX4从放弃到精通(二十四):自定义机型
How to write this with data and proc without SQL
How much is the bonus of China Patent Award, with a subsidy of 1million yuan
北京平谷区高新技术企业培育支持标准,补贴10万
Hooks' design philosophy
[interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV
Direct insert sort and shell sort
中间件_Redis_05_Redis的持久化
中间件_Redis_06_Redis的事务
SAS判别分析(Bayes准则和proc discrim过程)