0
如何自動清除instanceMap的鍵和值;當getInstance()API返回的Conf對象使用WeakHashMap和WeakReference ...進行垃圾回收時?WeakHashMap和WeakReference
//single conference instance per ConferenceID
class Conf {
private static HashMap<String, Conf> instanceMap = new HashMap<String, Conf>;
/*
* Below code will avoid two threads are requesting
* to create conference with same confID.
*/
public static Conf getInstance(String confID){
//This below synch will ensure singleTon created per confID
synchronized(Conf.Class) {
Conf conf = instanceMap.get(confID);
if(conf == null) {
conf = new Conf();
instanceMap.put(confID, conf);
}
return conf;
}
}
}
謝謝!彼得。但是,你能否合併兩者?我不想要死鑰匙,並且我不想運行單獨的線程來使用迭代清理死鑰匙。 –
@KanagaveluSugumar您可以通過將循環放入方法中。 –