在多線程中,使用ArrayList 進行remove操做時,會報異常。改用CopyOnWriteArrayList,若再進Collections.sort(copyOnWriteArrayList, myComparator)時,會報異常:java
java.lang.UnsupportedOperationException
由於CopyOnWriteArrayList不支持set()操做。多線程
解決辦法是:spa
List temp = Arrays.asList(copyOnWriteArrayList.toArray()); Collections.sort(temp); copyOnwriteArrayList.clear(); copyOnWriteArrayList.addAll(temp);