天天看小說

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

}

}

第90章第134章第128章第103章第52章第225章第48章第16章第209章第90章第50章第194章第17章第5章第90章第24章第166章第160章第24章第171章第85章第170章第142章第228章第2章第193章第30章第95章第76章第55章第202章第56章第62章第172章第139章第219章第181章第105章第114章第61章第161章第19章第144章第175章第219章第54章第40章第218章第135章第14章第21章第87章第203章第216章第154章第60章第73章第153章第133章第76章第124章第125章第211章第44章第104章第124章第58章第226章第135章第207章第60章第17章第32章第121章第8章第81章第107章第94章第151章第13章第220章第202章第117章第157章第164章第119章第122章第8章第151章第147章第72章第31章第166章第112章第102章第221章第106章第15章第213章第111章
第90章第134章第128章第103章第52章第225章第48章第16章第209章第90章第50章第194章第17章第5章第90章第24章第166章第160章第24章第171章第85章第170章第142章第228章第2章第193章第30章第95章第76章第55章第202章第56章第62章第172章第139章第219章第181章第105章第114章第61章第161章第19章第144章第175章第219章第54章第40章第218章第135章第14章第21章第87章第203章第216章第154章第60章第73章第153章第133章第76章第124章第125章第211章第44章第104章第124章第58章第226章第135章第207章第60章第17章第32章第121章第8章第81章第107章第94章第151章第13章第220章第202章第117章第157章第164章第119章第122章第8章第151章第147章第72章第31章第166章第112章第102章第221章第106章第15章第213章第111章
主站蜘蛛池模板: 桓仁| 河南省| 西乡县| 榕江县| 湄潭县| 江口县| 怀仁县| 陆川县| 常宁市| 沛县| 伊通| 锡林浩特市| 漳浦县| 武邑县| 即墨市| 江源县| 泸州市| 高清| 吉安县| 湘阴县| 中卫市| 杭锦旗| 济宁市| 义马市| 瑞金市| 朝阳县| 拉孜县| 微博| 景泰县| 庆安县| 陈巴尔虎旗| 荆门市| 本溪市| 新田县| 曲靖市| 泽普县| 锡林郭勒盟| 贺兰县| 唐海县| 安乡县| 上栗县|