包java.util.concurrent.ThreadPoolExecutor
具有以下方法:
public void purge() {
final BlockingQueue<Runnable> q = workQueue;
try {
Iterator<Runnable> it = q.iterator();
while (it.hasNext()) {
Runnable r = it.next();
if (r instanceof Future<?> && ((Future<?>)r).isCancelled())
it.remove();
}
} catch (ConcurrentModificationException fallThrough) {
// Take slow path if we encounter interference during traversal.
// Make copy for traversal and call remove for cancelled entries.
// The slow path is more likely to be O(N*N).
for (Object r : q.toArray())
if (r instanceof Future<?> && ((Future<?>)r).isCancelled())
q.remove(r);
}
tryTerminate(); // In case SHUTDOWN and now empty
}
在Java文档中,我看到有一个“ConcurrentModificationException”异常,但是文档中同时指出:返回的迭代器是“弱一致性”迭代器,在遍历元素时绝不会抛出ConcurrentModificationException异常,并保证按照迭代器构造时元素的存在顺序来遍历元素,并且可能(但不能保证)反映构造后的任何修改。请告诉我如何理解。