Example usage for java.util.concurrent BlockingQueue remove

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

Introduction

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

Prototype

boolean remove(Object o);

Source Link

Document

Removes a single instance of the specified element from this queue, if it is present.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int capacity = 10;
    BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);

    for (int i = 0; i < 10; i++) {
        queue.add(i);//from w w  w . j  a  va  2  s  .c  o  m
    }

    System.out.println(queue.remove(new Integer(0)));
}

From source file:com.sm.store.cluster.Connection.java

public void remoteFromQueue(BlockingQueue<Connection> queue) {
    lock.lock();//from  ww  w  .  j  av a  2 s.co m
    try {
        boolean flag = queue.remove(this);
        if (flag)
            inQueue = false;
        else
            logger.warn("unable to remove from reconnectQueue " + toString());
    } finally {
        lock.unlock();
    }
}

From source file:com.curecomp.primefaces.migrator.PrimefacesMigration.java

private static void findWidgetVarUsages(Path sourceFile, WidgetVarLocation widgetVarLocation,
        BlockingQueue<WidgetVarLocation> foundUsages, BlockingQueue<WidgetVarLocation> skippedUsages,
        BlockingQueue<WidgetVarLocation> unusedOrAmbiguous) throws IOException {
    try (BufferedReader br = Files.newBufferedReader(sourceFile, StandardCharsets.UTF_8)) {
        int lineNr = 0;
        String line;//w w  w. j  a  v  a 2s  .com
        while ((line = br.readLine()) != null) {
            lineNr++;

            int startIndex = 0;
            int endIndex = -1;

            while ((startIndex = line.indexOf(widgetVarLocation.widgetVar, endIndex + 1)) > -1) {
                endIndex = startIndex + widgetVarLocation.widgetVar.length();

                if (sourceFile.equals(widgetVarLocation.location) && lineNr == widgetVarLocation.lineNr
                        && startIndex == widgetVarLocation.columnNr) {
                    continue;
                }

                WidgetVarLocation usage = new WidgetVarLocation(widgetVarLocation.widgetVar, sourceFile, lineNr,
                        startIndex, line);

                // Only look at lines that use the word as a whole and not just as a part
                if ((startIndex == 0 || !Character.isJavaIdentifierStart(line.charAt(startIndex - 1)))
                        && (line.length() == endIndex
                                || !Character.isJavaIdentifierPart(line.charAt(endIndex)))) {
                    // We skip usages that occur as the last word of a line or usages that don't call methods directly
                    if (endIndex == line.length() || endIndex < line.length() && line.charAt(endIndex) != '.') {
                        skippedUsages.add(usage);
                    } else {
                        foundUsages.add(usage);
                    }
                } else {
                    skippedUsages.add(usage);
                }

                unusedOrAmbiguous.remove(widgetVarLocation);
            }
        }
    }
}

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

/**
 * Drains the task queue into a new list, normally using drainTo. But if the
 * queue is a DelayQueue or any other kind of queue for which poll or
 * drainTo may fail to remove some elements, it deletes them one by one.
 *///  ww w. j av a  2  s . co  m
private List<Runnable> drainQueue() {
    BlockingQueue<Runnable> q = workQueue;
    List<Runnable> taskList = new ArrayList<Runnable>();
    q.drainTo(taskList);
    if (!q.isEmpty()) {
        for (Runnable r : q.toArray(new Runnable[0])) {
            if (q.remove(r))
                taskList.add(r);
        }
    }
    return taskList;
}

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./* w w w. j  av  a 2s. c  om*/
 */
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
}