Example usage for java.util.concurrent ExecutorService awaitTermination

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

Introduction

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

Prototype

boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;

Source Link

Document

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Usage

From source file:eu.freme.bpt.service.AbstractService.java

public void run(final FailurePolicy failurePolicy, final int nrThreads, final Callback callback) {
    logger.info("Running service {}", this.getClass().getName());
    ExecutorService executorService = Executors.newFixedThreadPool(nrThreads);
    Unirest.setTimeouts(30000, 300000); // TODO: configurable?
    while (ioIterator.hasNext()) {
        final IO io = ioIterator.next();

        executorService.submit(() -> {
            try (final InputStream inputStream = io.getInputStream();
                    final OutputStream outputStream = io.getOutputStream()) {
                byte[] input = IOUtils.toByteArray(inputStream);
                HttpResponse<InputStream> response = Unirest.post(endpoint).headers(headers)
                        .queryString(parameters).body(input).asBinary();
                if (response.getStatus() == 200) {
                    logger.debug("Request alright.");
                    try (InputStream responseInput = response.getBody()) {
                        IOUtils.copy(responseInput, outputStream);
                        callback.onTaskComplete(io.getInputFile(), io.getOutputFile());
                    } catch (IOException e) {
                        logger.error("Error while writing response.", e);
                        callback.onTaskFails(io.getInputFile(), io.getOutputFile(),
                                "Error while writing response. " + e.getMessage());
                        if (!failurePolicy.check()) {
                            System.exit(3);
                        }/*from w  w w  .  ja  v a 2  s.  c  om*/
                    }
                } else {
                    String body = IOUtils.toString(response.getBody());
                    String msg = "Error response from service " + endpoint + ": Status " + response.getStatus()
                            + ": " + response.getStatusText() + " - " + body;
                    logger.error(msg);
                    callback.onTaskFails(io.getInputFile(), io.getOutputFile(), msg);
                    if (!failurePolicy.check()) {
                        System.exit(3);
                    }
                }
            } catch (Exception e) {
                logger.error("Request to {} failed." + endpoint, e);
                callback.onTaskFails(io.getInputFile(), io.getOutputFile(),
                        "Request to " + endpoint + " failed. " + e.getMessage());
                if (!failurePolicy.check()) {
                    System.exit(3);
                }
            }
        });
    }
    executorService.shutdown();
    try {
        executorService.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
        logger.warn("Waiting on termination interrupted.");
    }
    callback.onBatchComplete();
}

From source file:org.apache.usergrid.rest.UniqueValuesPerformanceIT.java

@Test
@Ignore("Intended for use against  prod-like cluster")
public void testBasicOperation() throws Exception {

    int numUsers = 1000;
    int numThreads = 100;
    int poolsize = 50;

    ExecutorService execService = Executors.newFixedThreadPool(poolsize);

    Multimap<String, Form> usersCreated = Multimaps
            .synchronizedListMultimap(ArrayListMultimap.create(numUsers, 1));

    Client client = ClientBuilder.newClient();

    String randomizer = RandomStringUtils.randomAlphanumeric(8);

    String[] targetHosts = { "http://macsnoopdave2013:8080", "http://macsnoopdave2010:9090" };

    final MetricRegistry metrics = new MetricRegistry();
    final Timer responses = metrics.timer(name(UniqueValuesPerformanceIT.class, "responses"));
    long startTime = System.currentTimeMillis();

    for (int i = 0; i < numThreads; i++) {

        execService.submit(() -> {/*from  w w w .  j av a  2 s  .co  m*/

            for (int j = 0; j < numUsers / numThreads; j++) {

                // every user gets unique name, no duplicates in this test
                UUID uuid = UUID.randomUUID();
                String username = "uv_test_user_" + uuid;

                Form form = new Form();
                form.param("name", username);
                form.param("username", username);
                form.param("email", username + "@example.org");
                form.param("password", "s3cr3t");

                Timer.Context time = responses.time();
                try {
                    final String host = targetHosts[j % targetHosts.length];
                    WebTarget target = client.target(host).path("/management/users");

                    Response response = target.request()
                            .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED));

                    if (response.getStatus() == 200 || response.getStatus() == 201) {
                        usersCreated.put(username, form);
                        successCounter.incrementAndGet();

                    } else {
                        String responseBody = response.readEntity(String.class);
                        logger.error("User creation failed status {} - {}", response.getStatus(), responseBody);
                        errorCounter.incrementAndGet();
                    }

                } catch (Exception e) {
                    errorCounter.incrementAndGet();
                    logger.error("Error", e);
                }
                time.stop();
            }

        });
    }
    execService.shutdown();

    try {
        while (!execService.awaitTermination(30, TimeUnit.SECONDS)) {
            System.out.println("Waiting...");
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    long endTime = System.currentTimeMillis();

    logger.info("Total time {}s", (endTime - startTime) / 1000);

    DecimalFormat format = new DecimalFormat("##.###");

    logger.info(
            "Timed {} requests:\n" + "mean rate {}/s\n" + "min       {}s\n" + "max       {}s\n"
                    + "mean      {}s",
            responses.getCount(), format.format(responses.getMeanRate()),
            format.format((double) responses.getSnapshot().getMin() / 1000000000),
            format.format((double) responses.getSnapshot().getMax() / 1000000000),
            format.format(responses.getSnapshot().getMean() / 1000000000));

    logger.info("Error count {} ratio = {}", errorCounter.get(),
            (float) errorCounter.get() / (float) responses.getCount());

    logger.info("Success count = {}", successCounter.get());

    Assert.assertEquals(0, errorCounter.get());
    Assert.assertEquals(numUsers, successCounter.get());
}

From source file:org.apache.jackrabbit.oak.plugins.index.lucene.IndexCopierTest.java

@Test
public void cowPoolClosedWithTaskInQueue() throws Exception {
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    Directory baseDir = new CloseSafeDir();
    IndexDefinition defn = new IndexDefinition(root, builder.getNodeState());
    IndexCopier copier = new RAMIndexCopier(baseDir, executorService, getWorkDir());

    final Set<String> toPause = Sets.newHashSet();
    final CountDownLatch pauseCopyLatch = new CountDownLatch(1);
    Directory remote = new CloseSafeDir() {
        @Override/*  w ww.  j av a  2  s.c  om*/
        public IndexOutput createOutput(String name, IOContext context) throws IOException {
            if (toPause.contains(name)) {
                try {
                    pauseCopyLatch.await();
                } catch (InterruptedException ignore) {

                }
            }
            return super.createOutput(name, context);
        }
    };

    final Directory local = copier.wrapForWrite(defn, remote, false);
    toPause.add("t2");
    byte[] t1 = writeFile(local, "t1");
    byte[] t2 = writeFile(local, "t2");
    byte[] t3 = writeFile(local, "t3");
    byte[] t4 = writeFile(local, "t4");

    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    Thread closer = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                local.close();
            } catch (Throwable e) {
                e.printStackTrace();
                error.set(e);
            }
        }
    });

    closer.start();

    copier.close();
    executorService.shutdown();
    executorService.awaitTermination(100, TimeUnit.MILLISECONDS);

    pauseCopyLatch.countDown();
    closer.join();
    assertNotNull("Close should have thrown an exception", error.get());
}

From source file:org.apache.cassandra.service.StorageService.java

/** shuts node off to writes, empties memtables and the commit log. */
public synchronized void drain() throws IOException, InterruptedException, ExecutionException {
    ExecutorService mutationStage = StageManager.getStage(Stage.MUTATION);
    if (mutationStage.isTerminated()) {
        logger_.warn("Cannot drain node (did it already happen?)");
        return;/*w  w  w .j a v  a 2s. c  om*/
    }
    setMode("Starting drain process", true);
    Gossiper.instance.stop();
    setMode("Draining: shutting down MessageService", false);
    MessagingService.instance().shutdown();
    setMode("Draining: emptying MessageService pools", false);
    MessagingService.instance().waitFor();

    setMode("Draining: clearing mutation stage", false);
    mutationStage.shutdown();
    mutationStage.awaitTermination(3600, TimeUnit.SECONDS);

    // lets flush.
    setMode("Draining: flushing column families", false);
    List<ColumnFamilyStore> cfses = new ArrayList<ColumnFamilyStore>();
    for (String tableName : DatabaseDescriptor.getNonSystemTables()) {
        Table table = Table.open(tableName);
        cfses.addAll(table.getColumnFamilyStores());
    }
    totalCFs = remainingCFs = cfses.size();
    for (ColumnFamilyStore cfs : cfses) {
        cfs.forceBlockingFlush();
        remainingCFs--;
    }

    ColumnFamilyStore.postFlushExecutor.shutdown();
    ColumnFamilyStore.postFlushExecutor.awaitTermination(60, TimeUnit.SECONDS);

    CommitLog.instance.shutdownBlocking();

    // want to make sure that any segments deleted as a result of flushing are gone.
    DeletionService.waitFor();

    setMode("Node is drained", true);
}

From source file:org.apache.maven.plugin.invoker.AbstractInvokerMojo.java

/**
 * Runs the specified build jobs./*from   w  ww  . j a va2s .  com*/
 *
 * @param projectsDir The base directory of all projects, must not be <code>null</code>.
 * @param buildJobs   The build jobs to run must not be <code>null</code> nor contain <code>null</code> elements.
 * @throws org.apache.maven.plugin.MojoExecutionException
 *          If any build could not be launched.
 */
private void runBuilds(final File projectsDir, BuildJob[] buildJobs) throws MojoExecutionException {
    if (!localRepositoryPath.exists()) {
        localRepositoryPath.mkdirs();
    }

    //-----------------------------------------------
    // interpolate settings file
    //-----------------------------------------------

    File interpolatedSettingsFile = null;
    if (settingsFile != null) {
        if (cloneProjectsTo != null) {
            interpolatedSettingsFile = new File(cloneProjectsTo, "interpolated-" + settingsFile.getName());
        } else {
            interpolatedSettingsFile = new File(settingsFile.getParentFile(),
                    "interpolated-" + settingsFile.getName());
        }
        buildInterpolatedFile(settingsFile, interpolatedSettingsFile);
    }

    //-----------------------------------------------
    // merge settings file
    //-----------------------------------------------

    SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();

    File mergedSettingsFile;
    Settings mergedSettings = this.settings;
    if (mergeUserSettings) {
        if (interpolatedSettingsFile != null) {
            // Have to merge the specified settings file (dominant) and the one of the invoking Maven process
            Reader reader = null;
            try {
                reader = new XmlStreamReader(interpolatedSettingsFile);
                SettingsXpp3Reader settingsReader = new SettingsXpp3Reader();
                Settings dominantSettings = settingsReader.read(reader);

                // MINVOKER-137: NPE on dominantSettings.getRuntimeInfo()
                // DefaultMavenSettingsBuilder does the same trick
                if (dominantSettings.getRuntimeInfo() == null) {
                    RuntimeInfo rtInfo = new RuntimeInfo(dominantSettings);
                    rtInfo.setFile(interpolatedSettingsFile);
                    dominantSettings.setRuntimeInfo(rtInfo);
                }

                Settings recessiveSettings = cloneSettings();

                SettingsUtils.merge(dominantSettings, recessiveSettings, TrackableBase.USER_LEVEL);

                mergedSettings = dominantSettings;
                getLog().debug("Merged specified settings file with settings of invoking process");
            } catch (XmlPullParserException e) {
                throw new MojoExecutionException("Could not read specified settings file", e);
            } catch (IOException e) {
                throw new MojoExecutionException("Could not read specified settings file", e);
            } finally {
                IOUtil.close(reader);
            }
        }
    }
    if (this.settingsFile != null && !mergeUserSettings) {
        mergedSettingsFile = interpolatedSettingsFile;
    } else {
        try {
            mergedSettingsFile = File.createTempFile("invoker-settings", ".xml");

            FileWriter fileWriter = null;
            try {
                fileWriter = new FileWriter(mergedSettingsFile);
                settingsWriter.write(fileWriter, mergedSettings);
            } finally {
                IOUtil.close(fileWriter);
            }

            if (getLog().isDebugEnabled()) {
                getLog().debug("Created temporary file for invoker settings.xml: "
                        + mergedSettingsFile.getAbsolutePath());
            }
        } catch (IOException e) {
            throw new MojoExecutionException("Could not create temporary file for invoker settings.xml", e);
        }
    }
    final File finalSettingsFile = mergedSettingsFile;

    if (mavenHome != null) {
        actualMavenVersion = SelectorUtils.getMavenVersion(mavenHome);
    } else {
        actualMavenVersion = SelectorUtils.getMavenVersion();
    }
    scriptRunner.setGlobalVariable("mavenVersion", actualMavenVersion);

    if (javaHome != null) {
        resolveExternalJreVersion();
    } else {
        actualJreVersion = SelectorUtils.getJreVersion();
    }

    try {
        if (isParallelRun()) {
            getLog().info("use parallelThreads " + parallelThreads);

            ExecutorService executorService = Executors.newFixedThreadPool(parallelThreads);
            for (final BuildJob job : buildJobs) {
                executorService.execute(new Runnable() {
                    public void run() {
                        try {
                            runBuild(projectsDir, job, finalSettingsFile);
                        } catch (MojoExecutionException e) {
                            throw new RuntimeException(e.getMessage(), e);
                        }
                    }
                });
            }

            try {
                executorService.shutdown();
                // TODO add a configurable time out
                executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        } else {
            for (BuildJob job : buildJobs) {
                runBuild(projectsDir, job, finalSettingsFile);
            }
        }
    } finally {
        if (interpolatedSettingsFile != null && cloneProjectsTo == null) {
            interpolatedSettingsFile.delete();
        }
        if (mergedSettingsFile != null && mergedSettingsFile.exists()) {
            mergedSettingsFile.delete();
        }
    }
}

From source file:com.gs.collections.impl.parallel.SerialParallelLazyPerformanceTest.java

private void forEach(FastList<Integer> collection) {
    MutableList<Runnable> runnables = FastList.newList();
    runnables.add(() -> this.basicSerialForEachPerformance(collection, SERIAL_RUN_COUNT));
    int cores = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(cores);
    runnables.add(() -> {//from w ww  . j a  v  a2 s.  co  m
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicParallelLazyForEachPerformance(collection, "Lambda", item -> map.put(item, Boolean.TRUE),
                PARALLEL_RUN_COUNT, cores, service);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicParallelLazyForEachPerformance(collection, "Procedure", new Procedure<Integer>() {
            @Override
            public void value(Integer each) {
                map.put(each, Boolean.TRUE);
            }
        }, PARALLEL_RUN_COUNT, cores, service);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(collection, "Lambda", item -> map.put(item, Boolean.TRUE),
                PARALLEL_RUN_COUNT);
    });
    List<Integer> arrayList = new ArrayList<>(collection);
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(arrayList, "Lambda", item -> map.put(item, Boolean.TRUE),
                PARALLEL_RUN_COUNT);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(arrayList, "Consumer", new Consumer<Integer>() {
            @Override
            public void accept(Integer each) {
                map.put(each, Boolean.TRUE);
            }
        }, PARALLEL_RUN_COUNT);
    });
    this.shuffleAndRun(runnables);
    service.shutdown();
    try {
        service.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:CV.java

public CV(AbstractCELA la, AbstractLearningProblem lp, final AbstractReasonerComponent rs, int folds,
        boolean leaveOneOut) {
    //console rendering of class expressions
    ManchesterOWLSyntaxOWLObjectRendererImpl renderer = new ManchesterOWLSyntaxOWLObjectRendererImpl();
    ToStringRenderer.getInstance().setRenderer(renderer);
    ToStringRenderer.getInstance().setShortFormProvider(new SimpleShortFormProvider());

    // the training and test sets used later on
    List<Set<OWLIndividual>> trainingSetsPos = new LinkedList<Set<OWLIndividual>>();
    List<Set<OWLIndividual>> trainingSetsNeg = new LinkedList<Set<OWLIndividual>>();
    List<Set<OWLIndividual>> testSetsPos = new LinkedList<Set<OWLIndividual>>();
    List<Set<OWLIndividual>> testSetsNeg = new LinkedList<Set<OWLIndividual>>();

    // get examples and shuffle them too
    Set<OWLIndividual> posExamples;
    Set<OWLIndividual> negExamples;
    if (lp instanceof PosNegLP) {
        posExamples = OWLAPIConverter.getOWLAPIIndividuals(((PosNegLP) lp).getPositiveExamples());
        negExamples = OWLAPIConverter.getOWLAPIIndividuals(((PosNegLP) lp).getNegativeExamples());
    } else if (lp instanceof PosOnlyLP) {
        posExamples = OWLAPIConverter.getOWLAPIIndividuals(((PosNegLP) lp).getPositiveExamples());
        negExamples = new HashSet<OWLIndividual>();
    } else {//from www.ja v  a2 s. c om
        throw new IllegalArgumentException("Only PosNeg and PosOnly learning problems are supported");
    }
    List<OWLIndividual> posExamplesList = new LinkedList<OWLIndividual>(posExamples);
    List<OWLIndividual> negExamplesList = new LinkedList<OWLIndividual>(negExamples);
    Collections.shuffle(posExamplesList, new Random(1));
    Collections.shuffle(negExamplesList, new Random(2));

    // sanity check whether nr. of folds makes sense for this benchmark
    if (!leaveOneOut && (posExamples.size() < folds && negExamples.size() < folds)) {
        System.out.println("The number of folds is higher than the number of "
                + "positive/negative examples. This can result in empty test sets. Exiting.");
        System.exit(0);
    }
    //         
    if (leaveOneOut) {
        // note that leave-one-out is not identical to k-fold with
        // k = nr. of examples in the current implementation, because
        // with n folds and n examples there is no guarantee that a fold
        // is never empty (this is an implementation issue)
        int nrOfExamples = posExamples.size() + negExamples.size();
        for (int i = 0; i < nrOfExamples; i++) {
            // ...
        }
        System.out.println("Leave-one-out not supported yet.");
        System.exit(1);
    } else {
        // calculating where to split the sets, ; note that we split
        // positive and negative examples separately such that the 
        // distribution of positive and negative examples remains similar
        // (note that there are better but more complex ways to implement this,
        // which guarantee that the sum of the elements of a fold for pos
        // and neg differs by at most 1 - it can differ by 2 in our implementation,
        // e.g. with 3 folds, 4 pos. examples, 4 neg. examples)
        int[] splitsPos = calculateSplits(posExamples.size(), folds);
        int[] splitsNeg = calculateSplits(negExamples.size(), folds);

        //            System.out.println(splitsPos[0]);
        //            System.out.println(splitsNeg[0]);

        // calculating training and test sets
        for (int i = 0; i < folds; i++) {
            Set<OWLIndividual> testPos = getTestingSet(posExamplesList, splitsPos, i);
            Set<OWLIndividual> testNeg = getTestingSet(negExamplesList, splitsNeg, i);
            testSetsPos.add(i, testPos);
            testSetsNeg.add(i, testNeg);
            trainingSetsPos.add(i, getTrainingSet(posExamples, testPos));
            trainingSetsNeg.add(i, getTrainingSet(negExamples, testNeg));
        }

    }

    // run the algorithm
    if (multiThreaded && lp instanceof Cloneable && la instanceof Cloneable) {
        ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() - 1);
        for (int currFold = 0; currFold < folds; currFold++) {
            try {
                final AbstractLearningProblem lpClone = (AbstractLearningProblem) lp.getClass()
                        .getMethod("clone").invoke(lp);
                final Set<OWLIndividual> trainPos = trainingSetsPos.get(currFold);
                final Set<OWLIndividual> trainNeg = trainingSetsNeg.get(currFold);
                final Set<OWLIndividual> testPos = testSetsPos.get(currFold);
                final Set<OWLIndividual> testNeg = testSetsNeg.get(currFold);
                if (lp instanceof PosNegLP) {
                    ((PosNegLP) lpClone).setPositiveExamples(OWLAPIConverter.convertIndividuals(trainPos));
                    ((PosNegLP) lpClone).setNegativeExamples(OWLAPIConverter.convertIndividuals(trainNeg));
                } else if (lp instanceof PosOnlyLP) {
                    ((PosOnlyLP) lpClone).setPositiveExamples(
                            new TreeSet<Individual>(OWLAPIConverter.convertIndividuals(trainPos)));
                }
                final AbstractCELA laClone = (AbstractCELA) la.getClass().getMethod("clone").invoke(la);
                final int i = currFold;
                es.submit(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            validate(laClone, lpClone, rs, i, trainPos, trainNeg, testPos, testNeg);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            }
        }
        es.shutdown();
        try {
            es.awaitTermination(1, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } else {
        for (int currFold = 0; currFold < folds; currFold++) {
            final Set<OWLIndividual> trainPos = trainingSetsPos.get(currFold);
            final Set<OWLIndividual> trainNeg = trainingSetsNeg.get(currFold);
            final Set<OWLIndividual> testPos = testSetsPos.get(currFold);
            final Set<OWLIndividual> testNeg = testSetsNeg.get(currFold);

            if (lp instanceof PosNegLP) {
                ((PosNegLP) lp).setPositiveExamples(OWLAPIConverter.convertIndividuals(trainPos));
                ((PosNegLP) lp).setNegativeExamples(OWLAPIConverter.convertIndividuals(trainNeg));
            } else if (lp instanceof PosOnlyLP) {
                Set<Individual> convertIndividuals = OWLAPIConverter.convertIndividuals(trainPos);
                ((PosOnlyLP) lp).setPositiveExamples(new TreeSet<Individual>(convertIndividuals));
            }

            validate(la, lp, rs, currFold, trainPos, trainNeg, testPos, testNeg);
        }
    }

    outputWriter("");
    outputWriter("Finished " + folds + "-folds cross-validation.");
    outputWriter("runtime: " + statOutput(df, runtime, "s"));
    outputWriter("length: " + statOutput(df, length, ""));
    outputWriter("F-Measure on training set: " + statOutput(df, fMeasureTraining, "%"));
    outputWriter("F-Measure: " + statOutput(df, fMeasure, "%"));
    outputWriter("predictive accuracy on training set: " + statOutput(df, accuracyTraining, "%"));
    outputWriter("predictive accuracy: " + statOutput(df, accuracy, "%"));

}

From source file:mitm.common.tools.SendMail.java

private void sendMultiThreaded(final MailTransport mailSender, final MimeMessage message,
        final Address[] recipients) throws InterruptedException {
    ExecutorService threadPool = Executors.newCachedThreadPool();

    final Semaphore semaphore = new Semaphore(threads, true);

    final long startTime = System.currentTimeMillis();

    for (int i = 1; i <= count; i++) {
        long threadStart = System.currentTimeMillis();

        semaphore.acquireUninterruptibly();

        threadPool.execute(new Runnable() {
            @Override//w  w w.j ava  2s.  c o m
            public void run() {
                try {
                    MimeMessage clone = MailUtils.cloneMessage(message);

                    int sent = sentCount.incrementAndGet();

                    if (uniqueFrom) {
                        Address[] froms = clone.getFrom();

                        if (froms != null && froms.length > 0) {
                            clone.setFrom(
                                    new InternetAddress(sent + EmailAddressUtils.getEmailAddress(froms[0])));
                        }
                    }

                    mailSender.sendMessage(clone, recipients);

                    long timePassed = DateTimeUtils
                            .millisecondsToSeconds(System.currentTimeMillis() - startTime);

                    StrBuilder sb = new StrBuilder();

                    sb.append("Message\t" + sent + "\tsent.");

                    if (timePassed > 0) {
                        float msgPerSec = (float) sent / timePassed;

                        sb.append("\tmessages/second\t" + String.format("%.2f", msgPerSec));
                    }

                    logger.info(sb.toString());
                } catch (MessagingException e) {
                    logger.error("Error sending message.", e);
                } finally {
                    semaphore.release();
                }
            }
        });

        if (forceQuit.get()) {
            break;
        }

        if (throtllingSemaphore != null) {
            /* for throttling the sending of emails */
            throtllingSemaphore.acquire();
        } else {
            /* no throttling so use delay */
            long sleepTime = delay - (System.currentTimeMillis() - threadStart);

            if (sleepTime > 0) {
                Thread.sleep(sleepTime);
            }
        }
    }

    threadPool.shutdown();
    threadPool.awaitTermination(30, TimeUnit.SECONDS);

    waitForReceiveThreads();

    logger.info("Total sent: " + sentCount.intValue() + ". Total time: "
            + DateTimeUtils.millisecondsToSeconds(System.currentTimeMillis() - startTime) + " (sec.)");
}

From source file:plaid.compilerjava.CompilerCore.java

private void generateCode(List<CompilationUnit> cus, final PackageRep plaidpath) throws Exception {
    if (cc.isVerbose()) {
        System.out.println("Generating code.");
    }//w w w . ja v  a 2s  .c  o  m

    final List<File> allFiles = new ArrayList<File>();
    ExecutorService taskPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    for (final CompilationUnit cu : cus) {
        if (!cc.forceRecompile()) {
            boolean rebuild = false;
            for (Decl d : cu.getDecls()) {
                //System.out.println(d);
                StringBuilder packageName = new StringBuilder();
                for (String s : cu.getPackageName()) {
                    packageName.append(s);
                    packageName.append(System.getProperty("file.separator"));
                }
                File targetFile = new File(cc.getTempDir() + System.getProperty("file.separator") + packageName
                        + d.getName() + ".java");
                if (!targetFile.exists() || targetFile.lastModified() < cu.getSourceFile().lastModified()) {
                    rebuild = true;
                    break;
                }
            }
            if (!rebuild) {
                if (cc.isVerbose()) {
                    System.out.println("file up-to-date : " + cu.getSourceFile());
                }
                continue;
            }
            if (cc.isVerbose()) {
                System.out.println("Rebuild: " + cu.getSourceFile());
            }
        }
        Callable<Object> task = new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                try {
                    if (cc.isVerbose())
                        System.out.println("Generating code for:\n" + cu);
                    List<File> fileList = cu.codegen(cc, plaidpath);

                    synchronized (allFiles) {
                        allFiles.addAll(fileList);
                    }
                } catch (PlaidException p) {
                    System.err.println("Error while compiling " + cu.getSourceFile().toString() + ":");
                    System.err.println("");
                    printExceptionInformation(p);
                }
                return null;
            }
        };
        taskPool.submit(task);
    }
    taskPool.shutdown();
    while (!taskPool.isTerminated()) {
        taskPool.awaitTermination(1, TimeUnit.MINUTES);
    }

    if (!cc.isKeepTemporaryFiles()) {
        for (File f : allFiles) {
            f.deleteOnExit();
        }
    }

    if (cc.isVerbose()) {
        System.out.println("invoke Java compiler");
    }
    if (cc.isInvokeCompiler() && allFiles.size() > 0) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjectsFromFiles(allFiles);

        List<String> optionList = new ArrayList<String>();
        optionList.addAll(Arrays.asList("-target", "1.5"));
        // Set compiler's classpath to be same as the runtime's
        optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path")));
        // TODO: Add a separate compiler flag for this.
        optionList.addAll(Arrays.asList("-d", cc.getOutputDir()));
        //         optionList.add("-verbose");

        // Invoke the compiler
        CompilationTask task = compiler.getTask(null, null, null, optionList, null, fileObjects);
        Boolean resultCode = task.call();
        if (!resultCode.booleanValue())
            throw new RuntimeException("Error while compiling generated Java files.");
    }
}

From source file:es.upm.oeg.tools.quality.ldsniffer.eval.Evaluation.java

public void calculateMetrics() {

    Model model = ModelFactory.createDefaultModel();
    //TODO try to limit the size of the file being loaded
    try {/*from  ww  w.  j a v a 2  s  .co m*/
        model.read(url);
    } catch (RiotException e) {
        model = RDFDataMgr.loadModel(url + ".rdf");
    } catch (Exception e) {
        throw new BadRequestException(
                String.format("Unable to read the content from the uri - %s \n(%s)", url, e.getMessage()), e);
    }

    totalTriples = QueryUtils.getCountAsC(model, TOTAL_TRIPLES);
    iriSubjects = QueryUtils.getIriList(model, DISTINCT_IRI_SUBJECTS);
    iriPredicates = QueryUtils.getIriList(model, DISTINCT_IRI_PREDICATES);
    iriObjects = QueryUtils.getIriList(model, DISTINCT_IRI_OBJECTS);

    iriSet.addAll(iriSubjects);
    iriSet.addAll(iriPredicates);
    iriSet.addAll(iriObjects);

    final ExecutorService executor = Executors.newFixedThreadPool(5);

    iriSet.forEach(iri -> {
        executor.submit(() -> {

            HttpResponse cachedResponse = Executor.getCachedResponse(iri);

            if (cachedResponse == null) {

                Date date = new Date();

                CloseableHttpClient httpclient = HttpClients.createDefault();
                HttpHead head = new HttpHead(iri);
                String method = "HEAD";

                try (CloseableHttpResponse response = httpclient.execute(head);) {
                    StatusLine statusLine = response.getStatusLine();
                    int statusCode = statusLine.getStatusCode();
                    if (statusCode == HttpStatus.SC_METHOD_NOT_ALLOWED
                            || statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR
                            || statusCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                        HttpGet get = new HttpGet(iri);
                        method = "GET";
                        try (CloseableHttpResponse getResponse = httpclient.execute(get);) {
                            statusLine = getResponse.getStatusLine();
                            statusCode = statusLine.getStatusCode();
                        }
                    }
                    responseMap.put(iri, new HttpResponse(iri, method, statusCode, statusLine.getReasonPhrase(),
                            date, false));
                    if (statusCode >= 200 && statusCode < 300) {
                        derefIriCount.getAndIncrement();
                    }

                    Executor.putCachedResponse(iri, new HttpResponse(iri, method, statusCode,
                            statusLine.getReasonPhrase(), date, true));

                } catch (ConnectException e) {
                    logger.error("Connection timed out ...", e);
                    responseMap.put(iri, new HttpResponse(iri, method, -1, e.getMessage(), date, false));
                } catch (IOException e) {
                    logger.error("IO error occurred ..", e);
                    responseMap.put(iri, new HttpResponse(iri, method, -1, e.getMessage(), date, false));
                }
            } else {
                responseMap.put(iri, cachedResponse);
            }
        });
    });

    executor.shutdown();
    try {
        executor.awaitTermination(LDSnifferApp.getEvaluationTimeout(), TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        throw new ServerError("Interrupted ...");
    }

    iriSubjects.forEach(subject -> incrementDerefCount(subject, derefIriSubjectCount));

    iriPredicates.forEach(predicate -> incrementDerefCount(predicate, derefIriPredicateCount));

    iriObjects.forEach(object -> incrementDerefCount(object, derefIriObjectCount));

}