public class Hungry {
private byte[] data1 = new byte[1024*1024];
private byte[] data2 = new byte[1024*1024];
private byte[] data3 = new byte[1024*1024];
private byte[] data4 = new byte[1024*1024];
private Hungry(){
}
private final static Hungry HUNGRY =new Hungry();
public static Hungry getInstance(){
return HUNGRY;
}
}
public class LazyMan {
private static boolean panxoapyyuan = false;
private LazyMan() {
synchronized (LazyMan.class){
if (panxoapyyuan==false){
panxoapyyuan=true;
}else {
throw new RuntimeException("不要试图重复创建");
}
}
}
private volatile static LazyMan lazyMan;
public static LazyMan getInstance(){
if (lazyMan==null){
synchronized (LazyMan.class){
if (lazyMan==null){
lazyMan = new LazyMan();
}
}
}
return lazyMan;
}
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Constructor<LazyMan> declaredMethod = LazyMan.class.getDeclaredConstructor(null);
declaredMethod.setAccessible(true);
LazyMan lazyMan2 = declaredMethod.newInstance();
LazyMan lazyMan1 = declaredMethod.newInstance();
System.out.println(lazyMan1);
System.out.println(lazyMan2);
}
}

public class Holder {
private Holder() {
}
public static Holder getInstance(){
return InnerClass.HOLDER;
}
public static class InnerClass{
private static final Holder HOLDER = new Holder();
}
}
public enum EnumSingle {
INSTANCE;
public EnumSingle getInstance(){
return INSTANCE;
}
}
class Test{
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
EnumSingle instance1 = EnumSingle.INSTANCE;
Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class,int.class);
declaredConstructor.setAccessible(true);
EnumSingle instance2 = declaredConstructor.newInstance();
System.out.println(instance1);
System.out.println(instance2);
}
}