第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;

}

}

第115章第164章第224章第97章第158章第64章第151章第134章第21章第183章第62章第33章第143章第214章第39章第117章第179章第161章第19章第55章第182章第34章第57章第11章第98章第66章第190章第1章第183章第96章第80章第60章第124章第57章第172章第185章第206章第210章第193章第83章第119章第34章第140章第31章第10章第28章第115章第16章第112章第16章第61章第105章第207章第129章第26章第157章第102章第200章第202章第127章第188章第16章第19章第135章第81章第114章第223章第199章第126章第87章第8章第107章第114章第3章第28章第193章第87章第19章第132章第119章第43章第134章第39章第111章第145章第173章第193章第227章第16章第189章第177章第78章第153章第95章第117章第2章第33章第5章第209章
第115章第164章第224章第97章第158章第64章第151章第134章第21章第183章第62章第33章第143章第214章第39章第117章第179章第161章第19章第55章第182章第34章第57章第11章第98章第66章第190章第1章第183章第96章第80章第60章第124章第57章第172章第185章第206章第210章第193章第83章第119章第34章第140章第31章第10章第28章第115章第16章第112章第16章第61章第105章第207章第129章第26章第157章第102章第200章第202章第127章第188章第16章第19章第135章第81章第114章第223章第199章第126章第87章第8章第107章第114章第3章第28章第193章第87章第19章第132章第119章第43章第134章第39章第111章第145章第173章第193章第227章第16章第189章第177章第78章第153章第95章第117章第2章第33章第5章第209章