Re: [蟲?]ConcurrentModificationException

看板java作者 (wake up ...)時間13年前 (2012/07/08 16:58), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
ArrayList is fail-fast. In the scenario where one thread is acessing to the list whilst the other is trying to do modification, ConcurrentModificationException will be thrown. Applying Collection.synchronizedXXXX() or concurrent package can solve this problem. http://www.ibm.com/developerworks/java/library/j-jtp07233/index.html ※ 引述《stupid5566 (屎丟比5566)》之銘言: : public class ComicService { : private Collection<ComicModel> models = new ArrayList<ComicModel>(); : public void delete(String isbnCode){ : for (ComicModel model : models){ : if(model.getIsbnCode().equals(isbnCode)){ : models.remove(model); : } : } : } : } : 主程式: : public class ComicApp { : public static void main(String[] args) { : ComicService comicService = new ComicService(); : comicService.delete("003"); : } : } : 結果跳出 java.util.ConcurrentModificationException 錯誤 : Model裡面就是簡單的getIsbnCode() setIsbnCode() : 學長寫得一模一樣 但是可以成功刪除 : 出了什麼問題嗎 : 啾啾啾啾啾啾我!!!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.168.73.251
文章代碼(AID): #1F-KkrmY (java)
文章代碼(AID): #1F-KkrmY (java)