private static HashMap<String, FileInfo> sFileInfoObjectList = new CacheLinkedHashMap<String, FileInfo>();
public static synchronized FileInfo getFileInfoForProvider(...) {
FileInfo foundFileInfo = null;
(...)
foundFileInfo = sFileInfoObjectList.get(hashEntryKey);
(...)
sFileInfoObjectList.put(hashEntryKey, foundFileInfo);
(...)
}
public static synchronized void removeFileInfoForProvider(final int providerId) {
Thread thread = new Thread() {
@Override
public void run() {
Iterator<Entry<String, FileInfo>> it = sFileInfoObjectList.entrySet().iterator();
while (it.hasNext()) {
Entry<String, FileInfo> pair = it.next();
FileInfo info = pair.getValue();
if (providerId == info.mProvider) {
it.remove();
}
}
}
};
}
我在 run() 方法中收到 ConccurentModificationException。我尝试了以下,但没有奏效:
public void run() {
synchronized(sFileInfoObjectList) {
Iterator<Entry<String, FileInfo>> it = sFileInfoObjectList.entrySet().iterator();
while (it.hasNext()) {
Entry<String, FileInfo> pair = it.next();
FileInfo info = pair.getValue();
if (providerId == info.mProvider) {
it.remove();
}
}
}
}