第55章

大B:“在多线程模式下,惰性初始化会使多个线程同时初始化该单体,造成一个JVM中多个单例类型的实例,如果这个单例类型的成员变量在运行过程中发生变化,会造成多个单例类型实例的不一致。”

小A:“那应该怎么办?”

大B:“加个同步修饰符:publicstaticsynchronizedSingletongetInstance()。这样就保证了线程的安全性。这种处理方式虽然引入了同步代码,但是因为这段同步代码只会在最开始的时候执行一次或多次,所以对整个系统的性能不会有影响。”

小A:“在更新属性的时候,会造成属性的读写不一致。那应该怎么处理?”

大B:“1、读者/写者的处理方式。设置一个读计数器,每次读取信息前,将计数器加1,读完后将计数器减1。使用notifyAll()解除在该对象上调用wait的线程阻塞状态。只有在读计数器为0时,才能更新数据,同时调用wait()方法要阻塞所有读属性的调用。2、采用‘影子实例’的办法。具体说,就是在更新属性时,直接生成另一个单例对象实例,这个新生成的单例对象实例将从数据库,文件或程序中读取最新的信息;然后将这些信息直接赋值给旧单例对象的属性。”

小A:“嘿嘿!师兄,能不能举例来看一下啊?”

大B:“好的。”

例子:

publicclassGlobalConfig{

privatestaticGlobalConfiginstance;

privateVectorproperties=null;

privatebooleanisUpdating=false;

privateintreadCount=0;

privateGlobalConfig(){

//LoadconfigurationinformationfromDBorfile

//Setvaluesforproperties

}

privatestaticsynchronizedvoidsyncInit(){

if(instance……null){

instance=newGlobalConfig();

}

}

publicstaticGlobalConfiggetInstance(){

if(instance……null){

syncInit();

}

returninstance;

}

publicsynchronizedvoidupdate(Stringpdata){

syncUpdateIn();

//Updateproperties

}

privatesynchronizedvoidsyncUpdateIn(){

while(readCount>0){

try{

wait();

}catch(Exceptione){

}

}

}

privatesynchronizedvoidsyncReadIn(){

readCount++;

}

privatesynchronizedvoidsyncReadOut(){

readCount——;

notifyAll();

}

publicVectorgetProperties(){

syncReadIn();

//Processdata

syncReadOut();

returnproperties;

}

}

publicclassGlobalConfig{

privatestaticGlobalConfiginstance;

privateVectorproperties=null;

privatebooleanisUpdating=false;

privateintreadCount=0;

privateGlobalConfig(){

//LoadconfigurationinformationfromDBorfile

//Setvaluesforproperties

}

privatestaticsynchronizedvoidsyncInit(){

if(instance……null){

instance=newGlobalConfig();

}

}

publicstaticGlobalConfiggetInstance(){

if(instance……null){

syncInit();

}

returninstance;

}

publicsynchronizedvoidupdate(Stringpdata){

syncUpdateIn();

//Updateproperties

}

privatesynchronizedvoidsyncUpdateIn(){

while(readCount>0){

try{

wait();

}catch(Exceptione){

}

}

}

privatesynchronizedvoidsyncReadIn(){

readCount++;

}

privatesynchronizedvoidsyncReadOut(){

readCount——;

notifyAll();

}

publicVectorgetProperties(){

syncReadIn();

//Processdata

syncReadOut();

returnproperties;

}

}

第126章第159章第71章第117章第93章第124章第71章第217章第108章第176章第146章第66章第129章第133章第110章第37章第118章第130章第189章第90章第68章第118章第44章第62章第193章第169章第149章第111章第77章第114章第219章第193章第56章第128章第16章第113章第149章第200章第36章第78章第44章第7章第156章第42章第7章第198章第46章第91章第35章第228章第73章第56章第27章第7章第3章第118章第12章第189章第101章第79章第146章第138章第216章第68章第158章第71章第175章第37章第121章第215章第20章第136章第63章第71章第58章第115章第35章第99章第113章第109章第119章第184章第105章第125章第216章第191章第7章第219章第33章第117章第71章第21章第105章第176章第72章第105章第107章第45章第115章
第126章第159章第71章第117章第93章第124章第71章第217章第108章第176章第146章第66章第129章第133章第110章第37章第118章第130章第189章第90章第68章第118章第44章第62章第193章第169章第149章第111章第77章第114章第219章第193章第56章第128章第16章第113章第149章第200章第36章第78章第44章第7章第156章第42章第7章第198章第46章第91章第35章第228章第73章第56章第27章第7章第3章第118章第12章第189章第101章第79章第146章第138章第216章第68章第158章第71章第175章第37章第121章第215章第20章第136章第63章第71章第58章第115章第35章第99章第113章第109章第119章第184章第105章第125章第216章第191章第7章第219章第33章第117章第71章第21章第105章第176章第72章第105章第107章第45章第115章