java.util.ConcurrentModificationException
阿新 • • 發佈:2018-12-12
出現異常的位置在After方法中遍歷集合的那行
//在測試單元中有如下程式碼 //遍歷Future載體 Thread thread=new Thread(()->{ listFuture.forEach(x->{ try { if (x.isDone()) { System.out.println("future:"+x+",result:"+x.get()); } list.add(Long.toString(System.currentTimeMillis())); } catch (Exception e) { System.out.println("丟擲異常:"+e); } }); }); thread.start(); //異常出現的位置在After單元中遍歷集合一行 @After public void clear(){ System.out.println("遍歷list集合"); list.forEach(System.out::println); }
官方說明:
問題出在遍歷集合時thread執行緒還在執行
解決方案:
將該執行緒轉交給執行緒池執行 ,並嘗試將主執行緒睡眠至確定執行緒執行完成後
重新測試後能用