java练习

单例模式

 

import java.io.*;

class singleton {
    
    Integer y = 2;
    
    private static volatile singleton instance = null;
    
    public static singleton getInstance() {
        if (instance == null) {
            synchronized(singleton.class) {
                if (instance == null) {
                    instance = new singleton();
                }
            }
        }
        return instance;
    }
    
    public singleton(){}
}

class test  
{
    
    public static void main (String[] args) throws java.lang.Exception
    {
        singleton x = singleton.getInstance();
        System.out.print(x.y);
    }
}

 

suqiandeMacBook-Pro:~/Tmp$ javac test.java
suqiandeMacBook-Pro:~/Tmp$ java test
2

suqiandeMacBook-Pro:~/Tmp$

 

上一篇:前后端数据交互axios


下一篇:快速搭建上位机的可视化系统配置层