当前位置:网站首页>Simple interest mode - lazy type
Simple interest mode - lazy type
2022-07-05 21:15:00 【Try to survive】
public class TestLazy {
public static void main(String[] args) {
Lazy l1 = Lazy.getInstance();
Lazy l2 = Lazy.getInstance();
System.out.println(l1 == l2);
System.out.println(l1);
System.out.println(l2);
}
}
public class TestLazy {
private static Lazy l1;
private static Lazy l2;
public static void main(String[] args) {
Thread t1 = new Thread(){
public void run(){
l1 = Lazy.getInstance();
}
};
Thread t2 = new Thread(){
public void run(){
l2 = Lazy.getInstance();
}
};
t1.start();
t2.start();
// The function of adding this code is , Guarantee l1 and l2 After the assignment is completed, compare
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(l1);
System.out.println(l2);
System.out.println(l1 == l2);
}
}
class Lazy{
//(2) Use a static variable to hold this unique instance
private static Lazy instance;// There's no way to new
//(1) Constructor privatization
private Lazy(){
}
// Right
/* //(3) When you get this object , And then to create public synchronized static Lazy getInstance(){ if(instance == null){ instance = new Lazy(); } return instance; }*/
//(3) When you get this object , And then to create
public static Lazy getInstance(){
if(instance == null){
// The function of this external condition is to improve efficiency When this object has been created , So the thread behind , No matter how many , There is no need to wait for the lock
synchronized (Lazy.class) {
//Lazy.class Get the... Of the current class Class object
if(instance == null){
// The conditions are for safety
instance = new Lazy();
}
}
}
return instance;
}
}
public class TestLazy {
public static void main(String[] args) {
LazyDemo.test();
LazyDemo instance = LazyDemo.getInstance();
}
}
class LazyDemo{
//(1) Constructor privatization
private LazyDemo(){
}
// Inner class
private static class Inner{
//(2) Use a static constant of the static inner class to save this unique object
public static final LazyDemo INSTANCE = new LazyDemo();
static{
System.out.println(" Static code block of inner class ");
}
}
// When we call this method , Get external class object , Will lead to the loading and initialization of internal classes , Will lead to INSTANCE Object creation , So it also belongs to the lazy type
public static LazyDemo getInstance (){
return Inner.INSTANCE;
}
// To test whether the inner class is loaded
public static void test(){
System.out.println(" Static methods of external classes ");
}
}
边栏推荐
- Traps in the explode function in PHP
- Pytorch实战——MNIST数据集手写数字识别
- Clion configures Visual Studio (MSVC) and JOM multi-core compilation
- Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
- Utils/index TS tool function
- Careercup its 1.8 serial shift includes problems
- 思特奇加入openGauss开源社区,共同推动数据库产业生态发展
- 第05章_存储引擎
- Five layer network protocol
- Using webassembly to operate excel on the browser side
猜你喜欢
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
Golang(1)|从环境准备到快速上手
LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
PVC plastic sheets BS 476-6 determination of flame propagation properties
MySQL InnoDB Architecture Principle
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
基於flask寫一個接口
浅聊我和一些编程语言的缘分
XML建模
Influence of oscilloscope probe on signal source impedance
随机推荐
基于flask写一个接口
ODPS 下一个map / reduce 准备
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
SQL series (basic) - Chapter 2 limiting and sorting data
MySQL InnoDB Architecture Principle
Sequence alignment
hdu2377Bus Pass(构建更复杂的图+spfa)
sql系列(基础)-第二章 限制和排序数据
Deep merge object deep copy of vant source code parsing
Web Service简单入门示例
Hdu2377bus pass (build more complex diagram +spfa)
CareerCup它1.8 串移包括问题
模式-“里氏替换原则”
Pytorch实战——MNIST数据集手写数字识别
Generics of TS
R语言【数据管理】
vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
Is Kai Niu 2980 useful? Is it safe to open an account
【日常训练】729. 我的日程安排表 I