import javax.management.relation.RelationNotFoundException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Lazy { private volatile static Lazy lazy; public Lazy() { if(lazy != null) { throw new RuntimeException("no reflect!"); } } public static Lazy getInstance(){ if(lazy == null){ synchronized (Lazy.class){ if(lazy == null) lazy = new Lazy(); } } return lazy; } public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { // new Thread(new Runnable() { // @Override // public void run() { // Lazy instance1 = Lazy.getInstance(); // System.out.println(instance1.hashCode()); // } // }).start(); // Lazy instance1 = declaredConstructor.newInstance(); for (int i = 0; i < 10; i++) { new Thread(new Runnable() { @Override public void run() { try { Constructor<Lazy> declaredConstructor = Lazy.class.getDeclaredConstructor(null); declaredConstructor.setAccessible(true); Lazy instance2 = declaredConstructor.newInstance(); System.out.println(instance2.hashCode()); } catch (InstantiationException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { e.printStackTrace(); } finally { System.out.println("finn"); } } }).start(); } } }