1575 |
|
* the presence of interference by other threads. |
1576 |
|
*/ |
1577 |
|
public void purge() { |
1578 |
< |
// Take slow path if we encounter interference during traversal |
1579 |
< |
boolean slow = false; |
1580 |
< |
BlockingQueue<Runnable> q = workQueue; |
1578 |
> |
final BlockingQueue<Runnable> q = workQueue; |
1579 |
|
try { |
1580 |
|
Iterator<Runnable> it = q.iterator(); |
1581 |
|
while (it.hasNext()) { |
1582 |
|
Runnable r = it.next(); |
1583 |
< |
if (r instanceof Future<?>) { |
1584 |
< |
Future<?> c = (Future<?>)r; |
1587 |
< |
if (c.isCancelled()) |
1588 |
< |
it.remove(); |
1589 |
< |
} |
1590 |
< |
} |
1591 |
< |
} |
1592 |
< |
catch (ConcurrentModificationException fallThrough) { |
1593 |
< |
slow = true; |
1594 |
< |
} |
1595 |
< |
if (slow) { |
1596 |
< |
// Make copy for traversal and call remove for cancelled entries |
1597 |
< |
Object[] entries = q.toArray(); |
1598 |
< |
for (int i = 0; i < entries.length; ++i) { |
1599 |
< |
Object e = entries[i]; |
1600 |
< |
if (e instanceof Future<?>) { |
1601 |
< |
Future<?> c = (Future<?>)e; |
1602 |
< |
if (c.isCancelled()) |
1603 |
< |
q.remove(c); |
1604 |
< |
} |
1583 |
> |
if (r instanceof Future<?> && ((Future<?>)r).isCancelled()) |
1584 |
> |
it.remove(); |
1585 |
|
} |
1586 |
+ |
} catch (ConcurrentModificationException fallThrough) { |
1587 |
+ |
// Take slow path if we encounter interference during traversal. |
1588 |
+ |
// Make copy for traversal and call remove for cancelled entries. |
1589 |
+ |
// The slow path is more likely to be O(N*N). |
1590 |
+ |
for (Object r : q.toArray()) |
1591 |
+ |
if (r instanceof Future<?> && ((Future<?>)r).isCancelled()) |
1592 |
+ |
q.remove(r); |
1593 |
|
} |
1594 |
|
|
1595 |
|
tryTerminate(); // In case SHUTDOWN and now empty |