package com.cavaness.quartzbook.chapter3;
public class Test {
private static Test instance = null;
public static synchronized Test getInstance() {
// 这个方法比上面有所改进,不用每次都进行生成对象,只是第一次
// 使用时生成实例,提高了效率!
if (instance == null) {
instance = new Test();
}
return instance;
}
public void method() {
System.out.println("The method of Test!");
}
}
class Test2 {
public static void main(String[] args) {
Test.getInstance().method();
}
}
单例2
最新推荐文章于 2024-10-16 20:56:17 发布