IntelliJ IDEA的Singleton模版的修改

来源:互联网 发布:手机数据是什么 编辑:程序博客网 时间:2024/04/30 13:50

原来的模版采用了所谓的懒汉模式,

现在改成有锁模式,可以在多线程情况下使用:

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end#parse("File Header.java")public class ${NAME}{    private static ${NAME} mInstance;    public static ${NAME} getInstance() {        if(mInstance==null){            synchronized(${NAME}.class){                if(mInstance==null){                    mInstance=new ${NAME}();                }            }        }        return mInstance;    }    private ${NAME}() {    }}