Example usage for java.util.concurrent ExecutorService shutdown

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

Introduction

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

Prototype

void shutdown();

Source Link

Document

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

Usage

From source file:es.urjc.etsii.code.UserSession.java

private void addFakeClients(UserSession presenterSession, JsonObject jsonMessage,
        final WebRtcEndpoint inputWebRtcEndpoint) {

    final String sessionNumber = jsonMessage.get("sessionNumber").getAsString();
    final int fakeClients = jsonMessage.getAsJsonPrimitive("fakeClients").getAsInt();
    final int timeBetweenClients = jsonMessage.getAsJsonPrimitive("timeBetweenClients").getAsInt();
    final boolean removeFakeClients = jsonMessage.getAsJsonPrimitive("removeFakeClients").getAsBoolean();
    final int playTime = jsonMessage.getAsJsonPrimitive("playTime").getAsInt();
    final String processing = jsonMessage.get("processing").getAsString();
    final int fakeClientsPerInstance = jsonMessage.getAsJsonPrimitive("fakeClientsPerInstance").getAsInt();

    new Thread(new Runnable() {
        @Override//from w w w  .  ja v a 2  s .c  o m
        public void run() {
            log.info("[Session number {} - WS session {}] Adding {} fake clients (rate {} ms) ", sessionNumber,
                    wsSession.getId(), fakeClients, timeBetweenClients);

            final CountDownLatch latch = new CountDownLatch(fakeClients);
            ExecutorService executor = Executors.newFixedThreadPool(fakeClients);
            for (int i = 0; i < fakeClients; i++) {
                waitMs(timeBetweenClients);
                final int j = i + 1;
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            addFakeClient(j, processing, inputWebRtcEndpoint, fakeClientsPerInstance);
                        } finally {
                            latch.countDown();
                        }
                    }
                });
            }

            try {
                latch.await();
            } catch (InterruptedException e) {
                log.warn("Exception waiting thread pool to be finished", e);
            }
            executor.shutdown();

            if (removeFakeClients) {
                log.info(
                        "[Session number {} - WS session {}] Waiting {} seconds with all fake clients connected",
                        sessionNumber, wsSession.getId(), playTime);

                for (List<MediaElement> list : mediaElementsInFakeMediaPipelineMap.values()) {
                    waitMs(playTime * 1000);
                    for (int i = 0; i < list.size() / 3; i++) {
                        if (i != 0) {
                            waitMs(timeBetweenClients);
                        }
                        log.info("[Session number {} - WS session {}] Releasing fake viewer {}", sessionNumber,
                                wsSession.getId(), i);
                        for (int j = 0; j < 3; j++) {
                            MediaElement mediaElement = list.get(3 * i + j);
                            if (mediaElement != null) {
                                log.debug("[Session number {} - WS session {}] Releasing {}", sessionNumber,
                                        wsSession.getId(), mediaElement);
                                mediaElement.release();
                                mediaElement = null;
                            }
                        }
                    }
                }
                mediaElementsInFakeMediaPipelineMap.clear();
                releaseFakeMediaPipeline();
            }
        }
    }).start();
}

From source file:broadwick.montecarlo.MonteCarlo.java

@Override
public void run() {
    log.trace("Starting Monte Carlo results producer thread");
    try {//  ww  w . ja v a  2  s .  co m
        final int poolSize = Runtime.getRuntime().availableProcessors();
        final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("MCScenarioProducer-%d")
                .setDaemon(true).build();
        final ExecutorService es = Executors.newFixedThreadPool(poolSize, threadFactory);
        final RNG generator = new RNG(RNG.Generator.Well44497b);

        final StopWatch sw = new StopWatch();
        sw.start();
        for (int i = 0; i < numSimulations; i++) {
            es.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        log.trace("Monte Carlo producer: creating scenario object");
                        final MonteCarloScenario scenario = simulation.copyOf();
                        final MonteCarloResults results = scenario
                                .run(generator.getInteger(0, Integer.MAX_VALUE - 1));
                        log.trace("Monte Carlo producer: generated results {}", results.getExpectedValue());
                        queue.put(results);
                    } catch (Exception e) {
                        log.error("Error running Monte Carlo simulation {}",
                                Throwables.getStackTraceAsString(e));
                    }
                }
            });
        }
        es.shutdown();
        while (!es.isTerminated()) {
            es.awaitTermination(1, TimeUnit.SECONDS);
        }
        queue.put(new Poison());

        sw.stop();
        log.info("Finished {} simulations in {}.", numSimulations, sw);
    } catch (Exception ex) {
        log.error("Monte Carlo simulation error: {}", Throwables.getStackTraceAsString(ex));
    }
}

From source file:com.linkedin.pinot.tools.admin.command.CreateSegmentCommand.java

@Override
public boolean execute() throws Exception {
    LOGGER.info("Executing command: {}", toString());

    // Load generator config if exist.
    final SegmentGeneratorConfig segmentGeneratorConfig;
    if (_generatorConfigFile != null) {
        segmentGeneratorConfig = new ObjectMapper().readValue(new File(_generatorConfigFile),
                SegmentGeneratorConfig.class);
    } else {/*  w ww .  j ava 2 s.  c  om*/
        segmentGeneratorConfig = new SegmentGeneratorConfig();
    }

    // Load config from segment generator config.
    String configDataDir = segmentGeneratorConfig.getDataDir();
    if (_dataDir == null) {
        if (configDataDir == null) {
            throw new RuntimeException("Must specify dataDir.");
        }
        _dataDir = configDataDir;
    } else {
        if (configDataDir != null && !configDataDir.equals(_dataDir)) {
            LOGGER.warn("Find dataDir conflict in command line and config file, use config in command line: {}",
                    _dataDir);
        }
    }

    FileFormat configFormat = segmentGeneratorConfig.getFormat();
    if (_format == null) {
        if (configFormat == null) {
            throw new RuntimeException("Format cannot be null in config file.");
        }
        _format = configFormat;
    } else {
        if (configFormat != _format && configFormat != FileFormat.AVRO) {
            LOGGER.warn("Find format conflict in command line and config file, use config in command line: {}",
                    _format);
        }
    }

    String configOutDir = segmentGeneratorConfig.getOutDir();
    if (_outDir == null) {
        if (configOutDir == null) {
            throw new RuntimeException("Must specify outDir.");
        }
        _outDir = configOutDir;
    } else {
        if (configOutDir != null && !configOutDir.equals(_outDir)) {
            LOGGER.warn("Find outDir conflict in command line and config file, use config in command line: {}",
                    _outDir);
        }
    }

    if (segmentGeneratorConfig.isOverwrite()) {
        _overwrite = true;
    }

    String configTableName = segmentGeneratorConfig.getTableName();
    if (_tableName == null) {
        if (configTableName == null) {
            throw new RuntimeException("Must specify tableName.");
        }
        _tableName = configTableName;
    } else {
        if (configTableName != null && !configTableName.equals(_tableName)) {
            LOGGER.warn(
                    "Find tableName conflict in command line and config file, use config in command line: {}",
                    _tableName);
        }
    }

    String configSegmentName = segmentGeneratorConfig.getSegmentName();
    if (_segmentName == null) {
        if (configSegmentName == null) {
            throw new RuntimeException("Must specify segmentName.");
        }
        _segmentName = configSegmentName;
    } else {
        if (configSegmentName != null && !configSegmentName.equals(_segmentName)) {
            LOGGER.warn(
                    "Find segmentName conflict in command line and config file, use config in command line: {}",
                    _segmentName);
        }
    }

    // Filter out all input files.
    File dir = new File(_dataDir);
    if (!dir.exists() || !dir.isDirectory()) {
        throw new RuntimeException("Data directory " + _dataDir + " not found.");
    }

    File[] files = dir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(_format.toString().toLowerCase());
        }
    });

    if ((files == null) || (files.length == 0)) {
        throw new RuntimeException("Data directory " + _dataDir + " does not contain "
                + _format.toString().toUpperCase() + " files.");
    }

    // Make sure output directory does not already exist, or can be overwritten.
    File outDir = new File(_outDir);
    if (outDir.exists()) {
        if (!_overwrite) {
            throw new IOException("Output directory " + _outDir + " already exists.");
        } else {
            FileUtils.deleteDirectory(outDir);
        }
    }

    // Set other generator configs from command line.
    segmentGeneratorConfig.setDataDir(_dataDir);
    segmentGeneratorConfig.setFormat(_format);
    segmentGeneratorConfig.setOutDir(_outDir);
    segmentGeneratorConfig.setOverwrite(_overwrite);
    segmentGeneratorConfig.setTableName(_tableName);
    segmentGeneratorConfig.setSegmentName(_segmentName);
    if (_schemaFile != null) {
        if (segmentGeneratorConfig.getSchemaFile() != null
                && !segmentGeneratorConfig.getSchemaFile().equals(_schemaFile)) {
            LOGGER.warn(
                    "Find schemaFile conflict in command line and config file, use config in command line: {}",
                    _schemaFile);
        }
        segmentGeneratorConfig.setSchemaFile(_schemaFile);
    }
    if (_readerConfigFile != null) {
        if (segmentGeneratorConfig.getReaderConfigFile() != null
                && !segmentGeneratorConfig.getReaderConfigFile().equals(_readerConfigFile)) {
            LOGGER.warn(
                    "Find readerConfigFile conflict in command line and config file, use config in command line: {}",
                    _readerConfigFile);
        }
        segmentGeneratorConfig.setReaderConfigFile(_readerConfigFile);
    }
    if (_enableStarTreeIndex) {
        segmentGeneratorConfig.setEnableStarTreeIndex(true);
    }
    if (_starTreeIndexSpecFile != null) {
        if (segmentGeneratorConfig.getStarTreeIndexSpecFile() != null
                && !segmentGeneratorConfig.getStarTreeIndexSpecFile().equals(_starTreeIndexSpecFile)) {
            LOGGER.warn(
                    "Find starTreeIndexSpecFile conflict in command line and config file, use config in command line: {}",
                    _starTreeIndexSpecFile);
        }
        segmentGeneratorConfig.setStarTreeIndexSpecFile(_starTreeIndexSpecFile);
    }

    ExecutorService executor = Executors.newFixedThreadPool(_numThreads);
    int cnt = 0;
    for (final File file : files) {
        final int segCnt = cnt;

        executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    SegmentGeneratorConfig config = new SegmentGeneratorConfig(segmentGeneratorConfig);
                    config.setInputFilePath(file.getAbsolutePath());
                    config.setSegmentName(_segmentName + "_" + segCnt);
                    config.loadConfigFiles();

                    final SegmentIndexCreationDriverImpl driver = new SegmentIndexCreationDriverImpl();
                    driver.init(config);
                    driver.build();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
        cnt += 1;
    }

    executor.shutdown();
    return executor.awaitTermination(1, TimeUnit.HOURS);
}

From source file:com.google.api.ads.adwords.jaxws.extensions.processors.onmemory.ReportProcessorOnMemory.java

/**
 * Downloads all the files from the API and process all the rows, saving the
 * data to the configured data base./*from  ww  w .j av a 2 s  .c o m*/
 * 
 * @param builder
 *            the session builder.
 * @param reportType
 *            the report type.
 * @param dateRangeType
 *            the date range type.
 * @param dateStart
 *            the start date.
 * @param dateEnd
 *            the ending date.
 * @param acountIdList
 *            the account IDs.
 * @param properties
 *            the properties resource.
 */
private <R extends Report> void downloadAndProcess(String userId, String mccAccountId,
        AdWordsSession.Builder builder, ReportDefinitionReportType reportType,
        ReportDefinitionDateRangeType dateRangeType, String dateStart, String dateEnd, Set<Long> acountIdList,
        Properties properties) {

    // Download Reports to local files and Generate Report objects
    LOGGER.info("\n\n ** Generating: " + reportType.name() + " **");
    LOGGER.info(" Processing reports...");

    ReportDefinition reportDefinition = getReportDefinition(reportType, dateRangeType, dateStart, dateEnd,
            properties);

    @SuppressWarnings("unchecked")
    Class<R> reportBeanClass = (Class<R>) this.csvReportEntitiesMapping.getReportBeanClass(reportType);

    final CountDownLatch latch = new CountDownLatch(acountIdList.size());
    ExecutorService executorService = Executors.newFixedThreadPool(numberOfReportProcessors);

    Stopwatch stopwatch = Stopwatch.createStarted();

    for (Long accountId : acountIdList) {
        LOGGER.trace(".");
        try {

            ModifiedCsvToBean<R> csvToBean = new ModifiedCsvToBean<R>();
            MappingStrategy<R> mappingStrategy = new AnnotationBasedMappingStrategy<R>(reportBeanClass);

            LOGGER.debug("Parsing account: " + accountId);

            RunnableProcessorOnMemory<R> runnableProcesor = new RunnableProcessorOnMemory<R>(accountId, builder,
                    reportDefinition, csvToBean, mappingStrategy, dateRangeType, dateStart, dateEnd,
                    mccAccountId, persister, reportRowsSetSize);

            runnableProcesor.setLatch(latch);
            executorService.execute(runnableProcesor);

        } catch (Exception e) {
            LOGGER.error("Ignoring account (Error when processing): " + accountId);
            e.printStackTrace();
        }
    }

    try {
        latch.await();
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage());
        e.printStackTrace();
    }
    executorService.shutdown();

    stopwatch.stop();
    LOGGER.info("*** Finished processing all reports in " + (stopwatch.elapsed(TimeUnit.MILLISECONDS) / 1000)
            + " seconds ***\n");
}

From source file:main.ScorePipeline.java

/**
 *
 * This method calculates similarities for MSRobin for each spectra on
 * yeast_human_spectra on the first data set against all yeast spectra on
 * the second data set//from  w w w  .jav a2 s. c o m
 *
 * thydMSnSpectra-yeast-human, tsolMSnSpectra-yeast,
 *
 * @param tsolMSnSpectra
 * @param thydMSnSpectra
 * @param bw
 * @param fragTol
 * @param precTol
 * @throws IllegalArgumentException
 * @throws ClassNotFoundException
 * @throws IOException
 * @throws MzMLUnmarshallerException
 * @throws NumberFormatException
 * @throws InterruptedException
 */
private static void calculate_MSRobins(ArrayList<MSnSpectrum> tsolMSnSpectra,
        ArrayList<MSnSpectrum> thydMSnSpectra, BufferedWriter bw, double fragTol, double precTol,
        int calculationOptionIntensityMSRobin, int msRobinCalculationOption)
        throws IllegalArgumentException, ClassNotFoundException, IOException, MzMLUnmarshallerException,
        NumberFormatException, InterruptedException {
    ExecutorService excService = Executors
            .newFixedThreadPool(ConfigHolder.getInstance().getInt("thread.numbers"));
    List<Future<SimilarityResult>> futureList = new ArrayList<>();
    for (MSnSpectrum thydMSnSpectrum : thydMSnSpectra) {
        Calculate_Similarity similarity = new Calculate_Similarity(thydMSnSpectrum, tsolMSnSpectra, fragTol,
                precTol, calculationOptionIntensityMSRobin, msRobinCalculationOption);
        Future future = excService.submit(similarity);
        futureList.add(future);
    }
    for (Future<SimilarityResult> future : futureList) {
        try {
            SimilarityResult get = future.get();
            String tmp_charge = get.getSpectrumChargeAsString(), spectrum = get.getSpectrumName();
            double tmp_precursor_mz = get.getSpectrumPrecursorMZ(),
                    msrobin = get.getScores().get(SimilarityMethods.MSRobin);
            if (msrobin == Double.MIN_VALUE) {
                LOGGER.info("The similarity for the spectrum " + spectrum
                        + " is too small to keep the record, therefore score is not computed.");
                // Means that score has not been calculated!
                //                    bw.write(tmp_Name + "\t" + tmp_charge + "\t" + tmpPrecMZ + "\t");
                //                    bw.write("NA" + "\n");
            } else {
                bw.write(spectrum + "\t" + tmp_charge + "\t" + tmp_precursor_mz + "\t"
                        + get.getSpectrumToCompare() + "\t" + msrobin + "\n");
            }
        } catch (InterruptedException | ExecutionException e) {
            LOGGER.error(e);
        }
    }
    excService.shutdown();

}

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

private void toList(FastList<Integer> collection) {
    MutableList<Runnable> runnables = FastList.newList();
    runnables.add(() -> this.basicSerialToListPerformance(collection, SERIAL_RUN_COUNT));
    int cores = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(cores);
    runnables//w w  w  . j  a v  a  2  s  .  c  o  m
            .add(() -> this.basicParallelLazyToListPerformance(collection, PARALLEL_RUN_COUNT, cores, service));
    runnables.add(() -> this.basicJava8ParallelLazyToListPerformance(collection, PARALLEL_RUN_COUNT));
    List<Integer> arrayList = new ArrayList<>(collection);
    runnables.add(() -> this.basicJava8ParallelLazyToListPerformance(arrayList, PARALLEL_RUN_COUNT));
    this.shuffleAndRun(runnables);
    service.shutdown();
    try {
        service.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.o2d.pkayjava.editor.proxy.ProjectManager.java

public void importFontIntoProject(Array<FileHandle> fileHandles, ProgressHandler progressHandler) {
    if (fileHandles == null) {
        return;/*from  w  w  w.j a  v  a2  s.  co m*/
    }
    String targetPath = currentWorkingPath + "/" + currentProjectVO.projectName + "/assets/orig/freetypefonts";
    handler = progressHandler;
    float perCopyPercent = 95.0f / fileHandles.size;
    for (FileHandle fileHandle : fileHandles) {
        if (!Overlap2DUtils.TTF_FILTER.accept(null, fileHandle.name())) {
            continue;
        }
        try {
            File target = new File(targetPath);
            if (!target.exists()) {
                File newFile = new File(targetPath);
                newFile.mkdir();
            }
            File fileTarget = new File(targetPath + "/" + fileHandle.name());
            FileUtils.copyFile(fileHandle.file(), fileTarget);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(perCopyPercent);
        changePercentBy(perCopyPercent);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(() -> {
            changePercentBy(100 - currentPercent);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            handler.progressComplete();
        });
        executor.shutdown();
    }
}

From source file:com.github.lindenb.mscheduler.MScheduler.java

protected int updateJobStatus(final Task task) {
    final StatusChecker call = createStatusChecker(task);
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    final Future<Integer> future = executor.submit(call);
    int return_status = -1;

    try {//from   w w  w. j a v a  2s.c o  m
        //allow 10 seconds to get status
        return_status = future.get(10, TimeUnit.SECONDS);

        return return_status;
    } catch (TimeoutException e) {
        future.cancel(true);
        LOG.error("Timeout for gettting job status and " + task);
        return -1;
    } catch (Exception e) {
        future.cancel(true);
        LOG.error("Failure:", e);
        return -1;
    } finally {
        executor.shutdown();
    }
}

From source file:com.hortonworks.hbase.BufferedMutatorExample.java

@Override
public int run(String[] args) throws InterruptedException, ExecutionException, TimeoutException {

    /** a callback invoked when an asynchronous write fails. */
    final BufferedMutator.ExceptionListener listener = new BufferedMutator.ExceptionListener() {
        @Override//from  w  w  w  . ja va2 s  .c  om
        public void onException(RetriesExhaustedWithDetailsException e, BufferedMutator mutator) {
            for (int i = 0; i < e.getNumExceptions(); i++) {
                LOG.info("Failed to send put " + e.getRow(i) + ".");
            }
        }
    };
    BufferedMutatorParams params = new BufferedMutatorParams(TABLE).listener(listener);

    //
    // step 1: create a single Connection and a BufferedMutator, shared by all worker threads.
    //
    Configuration conf = new Configuration();
    try (final Connection conn = ConnectionFactory.createConnection(conf);
            final BufferedMutator mutator = conn.getBufferedMutator(params)) {

        conf.set("hbase.zookeeper.quorum", "sandbox.hortonworks.com");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("zookeeper.znode.parent", "/hbase-unsecure");

        //        conf.set("hbase.zookeeper.quorum", "jetmaster2.jetnetname.artem.com,jetslave5.jetnetname.artem.com,jetslave1.jetnetname.artem.com");
        //        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //        conf.set("zookeeper.znode.parent", "/hbase-unsecure");

        /** worker pool that operates on BufferedTable instances */
        final ExecutorService workerPool = Executors.newFixedThreadPool(POOL_SIZE);
        List<Future<Void>> futures = new ArrayList<>(TASK_COUNT);

        for (int i = 0; i < TASK_COUNT; i++) {
            futures.add(workerPool.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    //
                    // step 2: each worker sends edits to the shared BufferedMutator instance. They all use
                    // the same backing buffer, call-back "listener", and RPC executor pool.
                    //
                    Put p = new Put(Bytes.toBytes("someRow"));
                    p.addColumn(FAMILY, Bytes.toBytes("someQualifier"), Bytes.toBytes("some value"));
                    mutator.mutate(p);
                    // do work... maybe you want to call mutator.flush() after many edits to ensure any of
                    // this worker's edits are sent before exiting the Callable
                    return null;
                }
            }));
        }

        //
        // step 3: clean up the worker pool, shut down.
        //
        for (Future<Void> f : futures) {
            f.get(5, TimeUnit.MINUTES);
        }
        workerPool.shutdown();
    } catch (IOException e) {
        // exception while creating/destroying Connection or BufferedMutator
        LOG.info("exception while creating/destroying Connection or BufferedMutator", e);
    } // BufferedMutator.close() ensures all work is flushed. Could be the custom listener is
      // invoked from here.
    return 0;
}

From source file:com.alibaba.otter.shared.arbitrate.zookeeper.DistributedLockTest.java

@Test
protected void test_try_lock() {
    ExecutorService exeucotr = Executors.newCachedThreadPool();
    final int count = 50;
    final CountDownLatch latch = new CountDownLatch(count);

    final DistributedLock[] nodes = new DistributedLock[count];
    for (int i = 0; i < count; i++) {
        final DistributedLock node = new DistributedLock(dir);
        nodes[i] = node;/*from   w w  w  .j ava  2s. c  o m*/
        exeucotr.submit(new Runnable() {

            public void run() {
                try {
                    while (node.tryLock() == false) {
                        Thread.sleep(100 + RandomUtils.nextInt(100));
                    }

                    System.out.println("id: " + node.getId() + " is leader: " + node.isOwner());
                } catch (InterruptedException e) {
                    want.fail();
                } catch (KeeperException e) {
                    want.fail();
                } finally {
                    latch.countDown();
                    try {
                        node.unlock();
                    } catch (KeeperException e) {
                        want.fail();
                    }
                }

            }
        });
    }

    try {
        latch.await();
    } catch (InterruptedException e) {
        want.fail();
    }

    exeucotr.shutdown();
}