大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;
}
}