package com.aaa.test;
/**
* 饿汉式单例模式
*
*
*/
public class OrderGen {
private OrderGen(){}
private static OrderGen intense=new OrderGen();
public static OrderGen getOrderGen(){
return intense;
}
public static void main(String[] args) {
OrderGen orderGen1=OrderGen.getOrderGen();
OrderGen orderGen2=OrderGen.getOrderGen();
System.out.println(orderGen1==orderGen2);
}
}
package com.aaa.test;
/**
* 懒汉式单例模式
*
*
*/
public class OrderGen2 {
private OrderGen2(){}
private static OrderGen2 intense=null;
public static synchronized OrderGen2 getOrderGen(){
if(intense==null){
intense=new OrderGen2();
}
return intense;
}
public static void main(String[] args) {
OrderGen2 orderGen1=OrderGen2.getOrderGen();
OrderGen2 orderGen2=OrderGen2.getOrderGen();
System.out.println(orderGen1==orderGen2);
}
}