Example usage for java.util.concurrent LinkedBlockingQueue remove

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

Introduction

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

Prototype

public 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:org.zoneproject.extractor.plugin.spotlight.App.java

public static void main(String[] args) {
    LinkedList<Item> itemsPending = new LinkedList<Item>();
    Prop[] fr = { new Prop(ZoneOntology.PLUGIN_LANG, "\"fr\"") };
    Prop[] en = { new Prop(ZoneOntology.PLUGIN_LANG, "\"en\"") };

    LinkedBlockingQueue<AnnotationThread> annotationThreads;
    HashMap<String, ArrayList<Prop>> propsToSave;
    propsPendingSave = new HashMap<String, ArrayList<Prop>>();
    while (true) {
        annotationThreads = new LinkedBlockingQueue<AnnotationThread>();

        while (true) {//while we can download items

            Item[] enItems = Database.getItemsNotAnotatedForPluginsWithDeps(PLUGIN_URI, en, SIM_DOWNLOADS / 2);
            Item[] frItems = Database.getItemsNotAnotatedForPluginsWithDeps(PLUGIN_URI, fr, SIM_DOWNLOADS / 2);
            Item[] items = (Item[]) ArrayUtils.addAll(enItems, frItems);

            if (items != null && items.length > 0) {
                //check if the item is in annotation process
                for (Item i : items) {
                    boolean exist = false;
                    for (AnnotationThread a : annotationThreads) {
                        if (a.item.getUri().equals(i.getUri())) {
                            exist = true;
                        }//from www .j  a va  2  s  .c  o m
                    }
                    if (!exist) {
                        itemsPending.add(i);
                    }
                }
            }
            if (itemsPending.isEmpty()) {
                break;
            }

            while (!itemsPending.isEmpty()) {

                //we add new thread until the limit length is thrown
                while (annotationThreads.size() < SIM_ANNOTATE && !itemsPending.isEmpty()) {
                    AnnotationThread newAnnot = new AnnotationThread(itemsPending.removeFirst());
                    newAnnot.start();
                    annotationThreads.add(newAnnot);
                }

                //try{
                //we try to end some terminated threads
                //synchronized(annotationThreads){
                for (AnnotationThread a : annotationThreads) {
                    if (!a.isAlive()) {
                        annotationThreads.remove(a);
                    } else if (a.getDuration() > LIMIT_TIME_FOR_DOWN) {
                        a.interrupt();
                    } else if (a.getDuration() > 10) {
                        logger.info("is alive[" + a.getDuration() + "]: " + a.item.getUri());
                    }
                    //try{Thread.currentThread().sleep(1000);}catch(Exception ie){}//TODO remove
                }
                //}
                //}catch(java.util.ConcurrentModificationException concurrentAccess){
                //    logger.warn("concurrent access!");
                //}

                if (annotationThreads.size() >= SIM_ANNOTATE) {
                    try {
                        Thread.currentThread().sleep(1000);
                    } catch (Exception ie) {
                    }
                }
            }

            logger.info("start saving");
            synchronized (propsPendingSave) {
                propsToSave = (HashMap<String, ArrayList<Prop>>) propsPendingSave.clone();
                propsPendingSave.clear();
            }
            Database.addAnnotations(propsToSave);
            logger.info("end saving");

        }

        logger.info("no more items to annotate");
        try {
            Thread.currentThread().sleep(1000);
        } catch (Exception ie) {
        }
    }
}