Example usage for java.util.concurrent ExecutorService execute

List of usage examples for java.util.concurrent ExecutorService execute

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService execute.

Prototype

void execute(Runnable command);

Source Link

Document

Executes the given command at some time in the future.

Usage

From source file:edu.cmu.lti.oaqa.bioasq.concept.retrieval.GoPubMedSeparateConceptRetrievalExecutor.java

@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
    AbstractQuery aquery = TypeUtil.getAbstractQueries(jcas).stream().findFirst().get();
    Collection<QueryConcept> qconcepts = TypeUtil.getQueryConcepts(aquery);
    List<ConceptSearchResult> concepts = Collections.synchronizedList(new ArrayList<>());
    ExecutorService es = Executors.newCachedThreadPool();
    for (QueryConcept qconcept : qconcepts) {
        String queryString = bopQueryStringConstructor.formatQueryConcept(qconcept)
                .replaceAll("[^A-Za-z0-9_\\-\"]+", " ");
        LOG.info("Query string: {}", queryString);
        for (BioASQUtil.Ontology ontology : BioASQUtil.Ontology.values()) {
            es.execute(() -> {
                try {
                    concepts.addAll(//from   ww w .j  av a  2s  . c  o m
                            BioASQUtil.searchOntology(service, jcas, queryString, pages, hits, ontology));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }
    es.shutdown();
    try {
        if (!es.awaitTermination(timeout, TimeUnit.MINUTES)) {
            LOG.warn("Timeout occurs for one or some concept retrieval services.");
        }
    } catch (InterruptedException e) {
        throw new AnalysisEngineProcessException(e);
    }
    Map<String, List<ConceptSearchResult>> onto2concepts = concepts.stream()
            .collect(groupingBy(ConceptSearchResult::getSearchId));
    for (Map.Entry<String, List<ConceptSearchResult>> entry : onto2concepts.entrySet()) {
        List<ConceptSearchResult> results = entry.getValue();
        LOG.info("Retrieved {} concepts from {}", results.size(), entry.getKey());
        if (LOG.isDebugEnabled()) {
            results.stream().limit(10).forEach(c -> LOG.debug(" - {}", TypeUtil.toString(c)));
        }
    }
    TypeUtil.rankedSearchResultsByScore(concepts, limit).forEach(ConceptSearchResult::addToIndexes);
}

From source file:org.commonjava.util.partyline.ConcurrentReadErrorsClearLocksTest.java

/**
 * Test that locks for mutiple reads clear correctly. This will setup an script of events for
 * a single file, where://w  w  w  .  jav a 2  s  .  c  o  m
 * <ol>
 *     <li>Multiple reads happen simultaneously, read the content, and close</li>
 *     <li>A single write at the end ensures the other locks are clear</li>
 * </ol>
 * @throws Exception
 */
/*@formatter:off*/
@BMRules(rules = {
        // When we try to init a new JoinableFile for INPUT, simulate an IOException from somewhere deeper in the stack.
        @BMRule(name = "new JoinableFile error", targetClass = "JoinableFile", targetMethod = "<init>", targetLocation = "ENTRY", condition = "$4 == false", action = "debug(\"Throwing test exception.\"); "
                + "throw new java.io.IOException(\"Test exception\")") })
/*@formatter:on*/
@BMUnitConfig(debug = true)
@Test
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(5);
    final File f = temp.newFile("child.txt");
    FileUtils.write(f, "test data");

    final CountDownLatch latch = new CountDownLatch(4);

    CountDownLatch readBeginLatch = new CountDownLatch(3);
    CountDownLatch readEndLatch = new CountDownLatch(3);

    final JoinableFileManager manager = new JoinableFileManager();
    final long start = System.currentTimeMillis();

    execs.execute(writer(manager, f, latch, readEndLatch));

    for (int i = 0; i < 3; i++) {
        final int k = i;
        execs.execute(reader(k, manager, f, latch, readBeginLatch, readEndLatch, null));
    }

    latch.await();
}

From source file:edu.stanford.nlp.parser.ensemble.Ensemble.java

public void run() throws IOException {
    List<Runnable> jobs = createJobs();

    boolean multiThreaded = false;
    if ((run.equalsIgnoreCase(Const.RUN_TRAIN) && multiThreadTrain)
            || (run.equalsIgnoreCase(Const.RUN_TEST) && multiThreadEval)) {
        multiThreaded = true;/*from w ww .j  av  a 2 s. co  m*/
    }

    String file_name;
    String phase_name;

    // reverse the training corpus
    if (run.equals(Const.RUN_TRAIN)) {
        file_name = trainCorpus;
        phase_name = "training";
    } // reverse the testing corpus
    else if (run.equals(Const.RUN_TEST)) {
        file_name = testCorpus;
        phase_name = "testing";
    } else {
        throw new RuntimeException("Unknown run mode: " + run);
    }

    if (rightToLeft) {
        File f = new File(file_name);
        File f1 = new File(workingDirectory + File.separator + f.getName());
        f1.deleteOnExit();
        FileUtils.copyFile(f, f1);

        if (rtl_pseudo_projective && run.equals(Const.RUN_TRAIN)) {
            String ppReversedFileName = workingDirectory + File.separator + f.getName() + ".pp";
            try {
                SystemLogger.logger().debug(
                        "Projectivise reversing " + phase_name + " corpus to " + ppReversedFileName + "\n");
                String input = f.getName();
                f = new File(ppReversedFileName);
                String output = f.getName();
                ProjectivizeCorpus.Projectivize(workingDirectory, input, output, "pp-reverse");
                f.deleteOnExit();
                f = new File("pp-reverse.mco");
                f.deleteOnExit();

                f = new File(ppReversedFileName);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("Error: cannot projectivize corpus");
            }
        }

        String reversedFileName = workingDirectory + File.separator + f.getName() + ".reversed";
        SystemLogger.logger().debug("Reversing " + phase_name + " corpus to " + reversedFileName + "\n");
        ReverseCorpus.reverseCorpus(f.getAbsolutePath(), reversedFileName);
        f = new File(reversedFileName);
        f.deleteOnExit();
    }

    if (ltr_pseudo_projective && run.equals(Const.RUN_TRAIN)) {
        File f = new File(file_name);
        File f1 = new File(workingDirectory + File.separator + f.getName());
        f1.deleteOnExit();
        FileUtils.copyFile(f, f1);
        String ppFileName = workingDirectory + File.separator + f.getName() + ".pp";
        try {
            SystemLogger.logger().debug("Projectivise " + phase_name + " corpus to " + ppFileName + "\n");
            String input = f.getName();
            f = new File(ppFileName);
            String output = f.getName();
            ProjectivizeCorpus.Projectivize(workingDirectory, input, output, "pp");
            f.deleteOnExit();
            f = new File("pp.mco");
            f.deleteOnExit();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Error: cannot projectivize corpus");
        }
    }

    if (multiThreaded) {
        ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
        for (Runnable job : jobs) {
            threadPool.execute(job);
        }
        threadPool.shutdown();
        this.waitForThreads(jobs.size());
    } else {
        for (Runnable job : jobs) {
            job.run();
        }
    }

    // run the actual ensemble model
    if (run.equalsIgnoreCase(Const.RUN_TEST)) {
        String outFile = workingDirectory + File.separator + outputPrefix + "." + modelName + "-ensemble";
        List<String> sysFiles = new ArrayList<String>();
        for (String baseModel : baseModels) {
            sysFiles.add(
                    (workingDirectory + File.separator + outputPrefix + "." + modelName + "-" + baseModel));
        }
        // generate the ensemble
        Eisner.ensemble(testCorpus, sysFiles, outFile, reparseAlgorithm);
        // score the ensemble
        Score s = Scorer.evaluate(testCorpus, outFile);
        if (s != null) {
            SystemLogger.logger().info(String.format("ensemble LAS: %.2f %d/%d\n", s.las, s.lcorrect, s.total));
            SystemLogger.logger().info(String.format("ensemble UAS: %.2f %d/%d\n", s.uas, s.ucorrect, s.total));
        }

        SystemLogger.logger().info("Ensemble output saved as: " + outFile + "\n");
    }

    SystemLogger.logger().info("DONE.\n");
}

From source file:org.apache.sentry.tests.e2e.dbprovider.TestConcurrentClients.java

/**
 * Test when concurrent HS2 clients talking to server,
 * Privileges are correctly created and updated.
 * @throws Exception/*  w  w w . j av  a 2  s.c o m*/
 */
@Test
public void testConccurentHS2Client() throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(NUM_OF_THREADS);
    final TestRuntimeState state = new TestRuntimeState();

    for (int i = 0; i < NUM_OF_TASKS; i++) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                LOGGER.info("Starting tests: create role, show role, create db and tbl, and create partitions");
                if (state.failed) {
                    return;
                }
                try {
                    Long startTime = System.currentTimeMillis();
                    Long elapsedTime = 0L;
                    while (Long.compare(elapsedTime, HS2_CLIENT_TEST_DURATION_MS) <= 0) {
                        String randStr = randomString(5);
                        String test_role = "test_role_" + randStr;
                        String test_db = "test_db_" + randStr;
                        String test_tb = "test_tb_" + randStr;
                        LOGGER.info("Start to test sentry with hs2 client with role " + test_role);
                        adminCreateRole(test_role);
                        adminShowRole(test_role);
                        createDbTb(ADMIN1, test_db, test_tb);
                        adminGrant(test_db, test_tb, test_role, USERGROUP1);
                        createPartition(USER1_1, test_db, test_tb);
                        adminCleanUp(test_db, test_role);
                        elapsedTime = System.currentTimeMillis() - startTime;
                        LOGGER.info("elapsedTime = " + elapsedTime);
                    }
                    state.setNumSuccess();
                } catch (Exception e) {
                    LOGGER.error("Exception: " + e);
                    state.setFirstException(e);
                }
            }
        });
    }
    executor.shutdown();
    while (!executor.isTerminated()) {
        Thread.sleep(1000); //millisecond
    }
    Throwable ex = state.getFirstException();
    assertFalse(ex == null ? "Test failed" : ex.toString(), state.failed);
    assertEquals(NUM_OF_TASKS, state.getNumSuccess());
}

From source file:models.Search.java

/**
 * @param request The clients request/*from  w ww .  j  a va  2s.  c  o m*/
 * @param serialization The wanted serialization of the returned data.
 * @return the chunks of the elasticsearch scroll scan query
 */
public Chunks<String> executeScrollScan(final Request request, final Serialization serialization) {
    validateSearchParameters();
    return new StringChunks() {
        @Override
        public void onReady(Chunks.Out<String> out) {
            setMessageOut(out);
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    doingScrollScanNow = true;
                    bulk(request, serialization);
                }
            });
            executorService.shutdown();
        }
    };
}

From source file:org.ros.internal.transport.queue.OutgoingMessageQueue.java

public OutgoingMessageQueue(MessageSerializer<T> serializer, ExecutorService executorService) {
    this.serializer = serializer;
    deque = MessageBlockingQueueFactory.newMessageBlockingQueue(DEQUE_CAPACITY, false);
    channelGroup = new DefaultChannelGroup();
    writer = new Writer();
    messageBufferPool = new MessageBufferPool();
    latchedBuffer = MessageBuffers.dynamicBuffer();
    mutex = new Object();
    latchMode = false;//from   w w w .  java  2  s  .com
    executorService.execute(writer);
}

From source file:org.commonjava.util.partyline.ConcurrentReadsClearLocksTest.java

/**
 * Test that locks for mutiple reads clear correctly. This will setup an script of events for
 * a single file, where:/*from  w  w  w .j av a  2  s . c  om*/
 * <ol>
 *     <li>Multiple reads happen simultaneously, read the content, and close</li>
 *     <li>A single write at the end ensures the other locks are clear</li>
 * </ol>
 * @throws Exception
 */
@Test
public void run() throws Exception {
    final ExecutorService execs = Executors.newFixedThreadPool(5);
    final File f = temp.newFile("child.txt");
    FileUtils.write(f, "test data");

    final CountDownLatch latch = new CountDownLatch(4);
    CountDownLatch readBeginLatch = new CountDownLatch(3);
    CountDownLatch readEndLatch = new CountDownLatch(3);
    final JoinableFileManager manager = new JoinableFileManager();
    manager.startReporting(5000, 5000);
    final long start = System.currentTimeMillis();

    execs.execute(writer(manager, f, latch, readEndLatch));

    for (int i = 0; i < 3; i++) {
        final int k = i;
        execs.execute(reader(k, manager, f, latch, readBeginLatch, readEndLatch, null));
    }

    latch.await();
}

From source file:io.anserini.index.IndexClueWeb09b.java

public int indexWithThreads(int numThreads) throws IOException, InterruptedException {

    System.out.println(/*from   ww  w .  java  2  s . c o  m*/
            "Indexing with " + numThreads + " threads to directory '" + indexPath.toAbsolutePath() + "'...");

    final Directory dir = FSDirectory.open(indexPath);

    final IndexWriterConfig iwc = new IndexWriterConfig(analyzer());

    iwc.setSimilarity(new BM25Similarity());
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    iwc.setRAMBufferSizeMB(256.0);
    iwc.setUseCompoundFile(false);
    iwc.setMergeScheduler(new ConcurrentMergeScheduler());

    final IndexWriter writer = new IndexWriter(dir, iwc);

    final ExecutorService executor = Executors.newFixedThreadPool(numThreads);

    List<Path> warcFiles = discoverWarcFiles(docDir);
    if (doclimit > 0 && warcFiles.size() < doclimit)
        warcFiles = warcFiles.subList(0, doclimit);

    for (Path f : warcFiles)
        executor.execute(new IndexerThread(writer, f));

    //add some delay to let some threads spawn by scheduler
    Thread.sleep(30000);
    executor.shutdown(); // Disable new tasks from being submitted

    try {
        // Wait for existing tasks to terminate
        while (!executor.awaitTermination(5, TimeUnit.MINUTES)) {
            Thread.sleep(1000);
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        executor.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }

    int numIndexed = writer.maxDoc();

    try {
        writer.commit();
        if (optimize)
            writer.forceMerge(1);
    } finally {
        writer.close();
    }

    return numIndexed;
}

From source file:org.pepstock.jem.node.StartUpSystem.java

/**
 * /*from   ww  w . j  av a  2 s.  c o  m*/
 * @param conf
 * @throws ConfigurationException
 */
private static void checkDataPaths() throws ConfigurationException {
    DistributedTask<List<String>> task = new DistributedTask<List<String>>(new GetDataPaths(),
            Main.getHazelcast().getCluster().getMembers().iterator().next());
    ExecutorService executorService = Main.getHazelcast().getExecutorService();
    executorService.execute(task);
    // gets result
    try {
        List<String> localDataPaths = Main.DATA_PATHS_MANAGER.getDataPathsNames();
        List<String> dataPaths = task.get();
        // checks if the amount is the same
        if (dataPaths.size() != localDataPaths.size()) {
            throw new ConfigurationException(NodeMessage.JEMC258E.toMessage()
                    .getFormattedMessage(dataPaths.size(), localDataPaths.size()));
        } else {
            for (String path : localDataPaths) {
                if (!dataPaths.contains(path)) {
                    throw new ConfigurationException(
                            NodeMessage.JEMC259E.toMessage().getFormattedMessage(path));
                }
            }
        }

    } catch (InterruptedException e) {
        throw new ConfigurationException(e.getMessage(), e);
    } catch (ExecutionException e) {
        throw new ConfigurationException(e.getMessage(), e);
    }
}

From source file:siddur.solidtrust.azure.AzureCarController.java

private ExecutorService consume(final BlockingQueue<List<AzureCar>> queue, int count) {
    Runnable consumer = new Runnable() {
        @Override//from   w w w  .  j a v a2 s  . co m
        public void run() {
            try {
                while (running) {
                    List<AzureCar> batch = queue.poll(100, TimeUnit.SECONDS);
                    if (batch != null) {
                        persister.saveBatch(batch);
                    }
                }
            } catch (InterruptedException e) {
                log4j.info(e.getMessage(), e);
            }
        }
    };

    ExecutorService pool = Executors.newFixedThreadPool(count);
    for (int i = 0; i < count; i++) {
        pool.execute(consumer);
    }
    return pool;
}