Reputation: 33
Package java.util.concurrent.ThreadPoolExecutor
has the following method:
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
}
There is an Exception ConcurrentModificationException
, but in Java doc I can see:
The returned iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.
Please tell me how to understand.
Upvotes: 3
Views: 480
Reputation: 6314
As you can see in the ThreadPoolExecutor
Constructor you can supply it with any BlockingQueue
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)
It can be your own implementation which doesn't have to be weakly consistent and though it doesn't have to throw ConcurrentModificationException
either this is the usual exception that is thrown so that's some defensive programming by Java developers.
Upvotes: 0