Example usage for java.util.concurrent PriorityBlockingQueue poll

List of usage examples for java.util.concurrent PriorityBlockingQueue poll

Introduction

In this page you can find the example usage for java.util.concurrent PriorityBlockingQueue poll.

Prototype

public E poll() 

Source Link

Usage

From source file:im.delight.imagescraper.ImageScraper.java

@Override
public void onImageCheckerFinished(PriorityBlockingQueue<ImageURL> imageURLs) {
    // COLLECT THE LARGEST IMAGE FILES BEGIN
    boolean imageSlotsAvailable = true;
    while (imageSlotsAvailable) { // while images available in queue
        ImageURL imageURL = imageURLs.poll(); // get the next image
        if (imageURL != null) { // if still images in queue
            if (imageURL.getFileSize() > 0) { // if image could be accessed
                imageSlotsAvailable = mOutput.addImageURL(imageURL.getURL()); // add it to result list
            }//from  w w w .j  av  a 2  s  .  c  o m
        } else { // if no more images in queue
            imageSlotsAvailable = false; // stop iterating
        }
    }
    // COLLECT THE LARGEST IMAGE FILES END

    if (mCallback != null) {
        mCallback.onFinished(mOutput); // notify the callback that the ImageScraper has finished and return its results
        mCallback = null; // unset the callback as we do not need to receive any further information
    } else {
        mPendingResult = mOutput;
    }
}

From source file:org.apache.geode.geospatial.client.ActorController.java

public void run() {
    final PriorityBlockingQueue<Actor> priorityQueue = new PriorityBlockingQueue<>(numberOfActors,
            (Comparator<Actor>) (o1, o2) -> (int) (o1.timeToAdvance() - o2.timeToAdvance()));

    //Slam in a couple tracks to keep the simulators busy
    int initialCount = numberOfSimulators * 2;
    for (int i = 0; i < initialCount; i++) {
        addActorToQueue(priorityQueue, i);
    }/*from   w  w  w .j a va2  s  . c o  m*/

    for (int i = 0; i < numberOfSimulators; i++) {
        new Thread(() -> {
            while (true) {
                Actor actor = priorityQueue.poll();
                long currentDelay = actor.timeToAdvance() - System.currentTimeMillis();
                actorDelay.update(currentDelay);
                if (currentDelay <= 0) {
                    try {
                        actor.advance();
                        Coordinate coordinate = actor.currentEvent();

                        LocationEvent locationEvent = new LocationEvent(coordinate.y, coordinate.x,
                                actor.getUid());
                        geoRegion.put(locationEvent.getUid(), locationEvent);
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                    priorityQueue.add(actor);
                } else {
                    priorityQueue.add(actor);
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    for (int i = initialCount; i < numberOfActors; i++) {
        //Now slowly trickle in more actors until we get up to the number of requested actors.
        //if we don't trickle them in then they are clustered at various starting points.
        try {
            Thread.sleep(newActorTimeout);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        addActorToQueue(priorityQueue, i);
        if (i % 1000 == 0) {
            logger.info("Injected {} drivers @ {}", i, new Date());
        }
    }
}

From source file:oculus.aperture.graph.aggregation.impl.ModularityAggregator.java

@Override
public void run() {

    logger.debug("Running kSnap clustering algorithm on " + nodeMap.size() + " nodes and " + linkMap.size()
            + " links...");

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();//from  w w w .j  a  v  a  2  s . c o m
    HashMap<String, ModularityNode> linklookup = new HashMap<String, ModularityAggregator.ModularityNode>();

    for (Node n : nodeMap.values()) {
        ModularityNode mn = new ModularityNode(n);
        linklookup.put(n.getId(), mn);
        groups.add(mn);

    }
    links = new ArrayList<ModularityLink>();

    for (Link l : linkMap.values()) {
        if (linklookup.containsKey(l.getSourceId()) && linklookup.containsKey(l.getTargetId())) {
            //if this is not true we have links pointing to an invalid node...
            ModularityLink ml = new ModularityLink(linklookup.get(l.getSourceId()),
                    linklookup.get(l.getTargetId()));
            links.add(ml);

            ModularityNode start = linklookup.get(l.getSourceId());
            ModularityNode end = linklookup.get(l.getSourceId());
            start.addLink(ml);
            end.addLink(ml);
        }

    }

    boolean notterminate = true;

    int linksize;

    while (notterminate) {
        final List<Future<?>> futures = new ArrayList<Future<?>>();
        notterminate = false;
        final PriorityBlockingQueue<ModularityLink> linksort = new PriorityBlockingQueue<ModularityLink>();
        linksize = links.size();
        final int itrsize = linksize / nThreads;
        for (int i = 0; i < nThreads; i++) {

            final int passval = i;

            Future<?> foo = executor.submit(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    boolean nt = false;
                    for (int lnknum = 0; lnknum < itrsize; lnknum++) {
                        ModularityLink ln = links.get(passval * itrsize + lnknum);
                        long nc = 0;
                        if (ln.source.neighbourcounts.containsKey(ln.target)) {
                            nc = ln.source.neighbourcounts.get(ln.target).intValue();
                        } else {
                            System.out.println("Oooops");
                        }

                        long q = nc - (ln.source.totalvolume * ln.target.totalvolume) / 2;

                        if (q > 0)
                            nt = true;
                        ln.q.set(q);
                        linksort.add(ln);
                    }
                    return nt;
                }
            });

            futures.add(foo);

        }

        for (Future<?> foo : futures) {
            try {
                notterminate = (Boolean) foo.get();
            } catch (InterruptedException interruptedCancellingAndSignalling) {
                Thread.currentThread().interrupt();
            } catch (ExecutionException wtf) {
                wtf.printStackTrace();
            }
        }

        if (!notterminate)
            break;
        //Now we take each link in the queue and add it to maximal matching 
        ConcurrentLinkedQueue<ModularityLink> maximalmatching = new ConcurrentLinkedQueue<ModularityAggregator.ModularityLink>();
        ConcurrentSkipListSet<ModularityNode> vertexcheck = new ConcurrentSkipListSet<ModularityAggregator.ModularityNode>();
        ModularityLink top = linksort.poll();
        maximalmatching.add(top);
        vertexcheck.add(top.source);
        vertexcheck.add(top.target);
        while (!linksort.isEmpty()) {
            ModularityLink nlnk = linksort.poll();
            if (nlnk.q.intValue() < 0)
                continue;

            if (vertexcheck.contains(nlnk.source) || vertexcheck.contains(nlnk.target))
                continue;
            maximalmatching.add(nlnk);
            vertexcheck.add(nlnk.source);
            vertexcheck.add(nlnk.target);
        }

        //Now we take all the pairs in maximal matching and fuse them
        for (ModularityLink ln : maximalmatching) {
            ModularityNode so = ln.source;
            ModularityNode tr = ln.target;
            so.assimilate(tr);
            groups.remove(tr);

            links.remove(ln);
        }
        linksize = links.size();
        if (linksize == 1)
            notterminate = false;
    }

    /*
    final List<Future<?>> futures = new ArrayList<Future<?>>();
            
    Future<?> foo = executor.submit(new Runnable(){
            
       @Override
       public void run() {
            
       }});
            
    futures.add(foo);
    */
    clusterSet = new ArrayList<Set<Node>>();

    for (ModularityNode g : groups) {

        if (cancel) {
            setStatusWaiting();
            return;
        }
        Set<Node> set = new HashSet<Node>();
        clusterSet.add(set);

        for (Node n : g.nodes) {

            if (cancel) {
                setStatusWaiting();
                return;
            }

            set.add(n);

        }

    }
    if (clusterer != null) {
        graphResult = clusterer.convertClusterSet(clusterSet);
    }
    stopWatch.stop();
    System.out.println("Finished Modularity clustering algorithm.");
    System.out.println("Algorithm took " + stopWatch.toString());//30 = 33.487
    stopWatch.reset();
    this.result = result;
}