当前位置:网站首页>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 ");
}
}
边栏推荐
- EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
- MySQL 千万数据量深分页优化, 拒绝线上故障!
- 模式-“里氏替换原则”
- EasyExcel的读写操作
- int GetMonth( ) const throw( ); What does throw () mean?
- 思特奇加入openGauss开源社区,共同推动数据库产业生态发展
- @Validated基础参数校验、分组参数验证和嵌套参数验证
- Determine the best implementation of horizontal and vertical screens
- 驱动壳美国测试UL 2043 符合要求有哪些?
- 启牛2980有没有用?开户安全吗、
猜你喜欢
示波器探头对信号源阻抗的影响
MySQL InnoDB Architecture Principle
Enclosed please find. Net Maui's latest learning resources
Evolution of zhenai microservice underlying framework from open source component encapsulation to self-development
CLion配置visual studio(msvc)和JOM多核编译
五层网络协议
【案例】元素的显示与隐藏的运用--元素遮罩
Clion configures Visual Studio (MSVC) and JOM multi-core compilation
Teach yourself to train pytorch model to Caffe (III)
LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
随机推荐
2022-07-03-cka- latest feedback from fans
@Validated basic parameter verification, grouping parameter verification and nested parameter verification
The reason why the ncnn converted model on raspberry pie 4B always crashes when called
Careercup its 1.8 serial shift includes problems
hdu2377Bus Pass(构建更复杂的图+spfa)
Influence of oscilloscope probe on measurement bandwidth
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
Aitm2-0002 12s or 60s vertical combustion test
Chapter 05_ Storage engine
PVC plastic sheets BS 476-6 determination of flame propagation properties
浅聊我和一些编程语言的缘分
让开发效率飞速提升的跨端方案
木板ISO 5660-1 热量释放速率摸底测试
Postgres establish connection and delete records
int GetMonth( ) const throw( ); What does throw () mean?
Get JS of the previous day (timestamp conversion)
Sequence alignment
Dictionary tree simple introductory question (actually blue question?)
js常用方法封装
Deep merge object deep copy of vant source code parsing