class MyTgreadScopeData{
//创建成一个单例模式,一首先有无参构造方法,是私有的
private MyTgreadScopeData(){}
//二 创建一个方法 饱汉模式的写法是先把对象new出来,然后返回值写成刚才创建的私有对象,
//意思就是调用我这个方法,我就给你创建好了,即使没有任何人调用这个方法,这个对象也创建好了
public static synchronized MyTgreadScopeData getInsance(){
if(instance == null){
instance = new MyTgreadScopeData();
}
return instance;
}
/*
* 饱汉模式的单例写法
private static MyTgreadScopeData instance = new MyTgreadScopeData();
*/
/*
* 饥汉模式的单例写法
*
* 刚开始没有创建对象,在上面getInsance()方法上先做判断,
* 如果返回值对象等于空null,我就给你创建一个对象,然后返回,
* 当在创建对象的时候相当于同一个对象创建了两次,这个是不允许的,
* 所以我们要在getInsance()方法上加上synchronized
*
* */
private static MyTgreadScopeData instance = null;
}