标签:closed effect illegal ati 反射 破坏 需求 声明 for
1 public class SingletonClass { 2 private static final SingletonClass instance = new SingletonClass(); 3 4 private SingletonClass(){} 5 6 public static SingletonClass getInstance(){ 7 return instance; 8 } 9 10 }
1 public class SingletonClass { 2 private static SingletonClass instance = null; 3 4 private SingletonClass(){} 5 6 public static SingletonClass getInstance(){ 7 if(null == instance){ 8 instance = new SingletonClass(); 9 } 10 11 return instance; 12 } 13 14 }
1 public class SingletonClass { 2 private static SingletonClass instance = null; 3 4 private SingletonClass(){} 5 6 public static SingletonClass getInstance(){ 7 synchronized (SingletonClass.class) { 8 if(null == instance){ 9 instance = new SingletonClass(); 10 } 11 } 12 13 return instance; 14 } 15 16 }
1 public class SingletonClass { 2 private static SingletonClass instance = null; 3 4 private SingletonClass(){} 5 6 public static SingletonClass getInstance(){ 7 if(null == instance){ 8 synchronized (SingletonClass.class) { 9 System.out.println("进入锁"); 10 if(null == instance){ 11 instance = new SingletonClass(); 12 } 13 } 14 } 15 return instance; 16 } 17 18 public static void main(String[] args) { 19 for (int i = 0; i < 10000; i++) { 20 SingletonClass.getInstance(); 21 } 22 System.out.println("处理完成"); 23 } 24 25 }
1 public class SingletonClass { 2 3 private SingletonClass(){} 4 5 private static class innerSingletonClass{ 6 private static final SingletonClass instance = new SingletonClass(); 7 } 8 9 public static SingletonClass getInstance(){ 10 return innerSingletonClass.instance; 11 } 12 13 }
1 public class SingletonClass implements Serializable{ 2 private static final long serialVersionUID = 1L; 3 4 private static SingletonClass instance = null; 5 6 private SingletonClass(){} 7 8 public static SingletonClass getInstance(){ 9 if(null == instance){ 10 synchronized (SingletonClass.class) { 11 if(null == instance){ 12 instance = new SingletonClass(); 13 } 14 } 15 } 16 return instance; 17 } 18 19 private Object readResolve(){//阻止生成新的实例,总是返回当前对象 20 return instance; 21 } 22 23 }
1 public enum SeasonEnum { 2 /** 3 * 在加载枚举类时初始化。 4 * 线程安全,与java的类加载机制相关。 5 * 全局唯一,反射不能更改。 6 * 反序列化后还是同一个对象 7 * */ 8 Spring(1); 9 10 private final int value;//可以像普通类一样定义Field,但是要定义在 枚举 后面,不然编译器会报错。 11 12 private SeasonEnum(int v) {//编译器只允许器定义成私有的。 13 value = v; 14 } 15 // private SeasonEnum() {//报错,不能重写无参构造方法 16 // 17 // } 18 19 public int getValue() { 20 return value; 21 } 22 23 public static void main(String[] args) throws Exception { 24 Class<SeasonEnum> cla = SeasonEnum.class; 25 Constructor<?>[] colls = cla.getDeclaredConstructors();//获得所有的构造器,拿到的是无参的创建枚举的构造方法。 26 System.out.println("获得的构造方法数量:" + colls.length);//获得的构造方法数量:1 27 for (Constructor<?> constructor : colls) { 28 System.out.println("构造方法名称:" + constructor.getName());//构造方法名称:testEnum.SeasonEnum 29 /** 30 * 运行报错 31 * Exception in thread "main" java.lang.IllegalArgumentException: Cannot reflectively create enum objects 32 * at java.lang.reflect.Constructor.newInstance(Constructor.java:402) 33 * */ 34 constructor.newInstance(0); 35 System.out.println(SeasonEnum.Spring); 36 } 37 38 } 39 }
标签:closed effect illegal ati 反射 破坏 需求 声明 for
原文地址:http://www.cnblogs.com/chenzl1024/p/6808352.html