当前位置:网站首页>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 ");
}
}
边栏推荐
- 判断横竖屏的最佳实现
- Evolution of zhenai microservice underlying framework from open source component encapsulation to self-development
- Reading and writing operations of easyexcel
- 水泥胶黏剂BS 476-4 不燃性测试
- PVC 塑料片BS 476-6 火焰传播性能测定
- 思特奇加入openGauss开源社区,共同推动数据库产业生态发展
- Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
- Simple getting started example of Web Service
- 显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
- MYSQL IFNULL使用功能
猜你喜欢
Why can't Chinese software companies produce products? Abandon the Internet after 00; Open source high-performance API gateway component of station B | weekly email exclusive to VIP members of Menon w
ArcGIS栅格重采样方法介绍
Realize the function of verifying whether the user has completed login when browsing the page
Reading and writing operations of easyexcel
Pytoch practice -- MNIST dataset handwritten digit recognition
Learning robots have no way to start? Let me show you the current hot research directions of robots
leetcode:1755. Sum of subsequences closest to the target value
浅聊我和一些编程语言的缘分
Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
随机推荐
Teach yourself to train pytorch model to Caffe (I)
Utils/index TS tool function
MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
EasyExcel的讀寫操作
LeetCode: Distinct Subsequences [115]
Prior knowledge of machine learning in probability theory (Part 1)
Enclosed please find. Net Maui's latest learning resources
PVC plastic sheets BS 476-6 determination of flame propagation properties
vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
Chapter 05_ Storage engine
vant 源码解析之 utils/index.ts 工具函数
概率论机器学习的先验知识(上)
Pytoch practice -- MNIST dataset handwritten digit recognition
Golang(1)|从环境准备到快速上手
Selenium gets the verification code image in DOM
《SAS编程和数据挖掘商业案例》学习笔记# 19
【案例】元素的显示与隐藏的运用--元素遮罩
Establishment of terminal security capability verification environment and penetration test records
Traps in the explode function in PHP
Hdu2377bus pass (build more complex diagram +spfa)