我有如下我的代碼和我得到ConcurrentModificationException的,尤其是在(String一個文件:文件)行ConcurrentModificationException的在HashSet的
做迭代的時候,不更改「文件」什麼,所以爲什麼會導致異常,我應該如何避免它?感謝您的任何建議!
int getTotalLength(final HashSet<String> files) {
int total = 0;
int len;
for (String file : files) {
len = getLength(file);
if (len != Long.MIN_VALUE) {
total += len;
}
}
return total;
}
int getLength(String file) {
int len = Long.MIN_VALUE;
if (file == null) {
return len;
}
File f = new File(file);
if (f.exists() && f.isFile()) {
len = f.length();
}
return size;
}
那麼你有任何其他線程,可能會在同一時間修改集? –
是的,這是可能的,但我已經使hashSet最終...不會阻止該設置被修改? –
ConcurrentModification在你的HashSet上,而不在你的文件上 –