Example usage for java.util.concurrent BlockingQueue toArray

List of usage examples for java.util.concurrent BlockingQueue toArray

Introduction

In this page you can find the example usage for java.util.concurrent BlockingQueue toArray.

Prototype

Object[] toArray();

Source Link

Document

Returns an array containing all of the elements in this collection.

Usage

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

/**
 * Tries to remove from the work queue all {@link Future} tasks that have
 * been cancelled. This method can be useful as a storage reclamation
 * operation, that has no other impact on functionality. Cancelled tasks are
 * never executed, but may accumulate in work queues until worker threads
 * can actively remove them. Invoking this method instead tries to remove
 * them now. However, this method may fail to remove tasks in the presence
 * of interference by other threads.//from   ww  w .  j  a  va  2s .  co  m
 */
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
}