当前位置:网站首页>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 ");
}
}
边栏推荐
猜你喜欢

The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application

基於flask寫一個接口

Golang (1) | from environmental preparation to quick start

Reading and writing operations of easyexcel

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

Influence of oscilloscope probe on measurement bandwidth

How to send samples when applying for BS 476-7 display? Is it the same as the display??

Golang(1)|从环境准备到快速上手
![Longest swing sequence [greedy practice]](/img/e1/70dc21b924232c7e5e3da023a4bed2.png)
Longest swing sequence [greedy practice]

使用WebAssembly在浏览器端操作Excel
随机推荐
The reason why the ncnn converted model on raspberry pie 4B always crashes when called
Selenium gets the verification code image in DOM
显示屏DIN 4102-1 Class B1防火测试要求
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
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
Binary search
Test of incombustibility of cement adhesives BS 476-4
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
EasyExcel的讀寫操作
Determine the best implementation of horizontal and vertical screens
Sequence alignment
Display DIN 4102-1 Class B1 fire test requirements
Teach yourself to train pytorch model to Caffe (III)
基于flask写一个接口
hdu2377Bus Pass(构建更复杂的图+spfa)
PVC 塑料片BS 476-6 火焰传播性能测定
Golang(1)|从环境准备到快速上手
XML建模
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session