方式一:双重检查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package app;
public class Single{
private static volatile Single instance;
private Single() throws Exception { if(instance != null){ throw new Exception(getClass()+"already instance"); } };
public static Single getInstance() throws Exception { if(instance == null){ synchronized (Single.class){ if (instance == null){ instance = new Single(); } } } return instance; }
}
|
方式二:静态代码块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package app;
public class SingleStatic {
private static SingleStatic singleStatic;
static { singleStatic = new SingleStatic(); }
private SingleStatic(){ if(singleStatic != null){ throw new RuntimeException(getClass()+" already instance"); } }
public static SingleStatic getInstance(){ return singleStatic; }
}
|
方式一测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public static void main(String[] args) throws Exception {
Single single = Single.getInstance(); Single single1 = Single.getInstance(); Single single2 = Single.getInstance();
System.out.println(single); System.out.println(single1); System.out.println(single2); System.out.println("--------");
Class<Single> singleClass = (Class<Single>) Class.forName("app.Single"); Constructor<Single> declaredConstructor = singleClass.getDeclaredConstructor(null); declaredConstructor.setAccessible(true); Single single3 = declaredConstructor.newInstance(); Single single4 = declaredConstructor.newInstance(); System.out.println(single3); System.out.println(single4);
}
|
结果:
若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏