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:org.yamj.core.service.ScanningScheduler.java

@Scheduled(initialDelay = 5000, fixedDelay = 45000)
public void scanMediaFiles() throws Exception {
    int maxThreads = configService.getIntProperty("yamj3.scheduler.mediafilescan.maxThreads", 1);
    if (maxThreads <= 0 || !mediaInfoService.isMediaInfoActivated()) {
        if (!messageDisabledMediaFiles) {
            messageDisabledMediaFiles = Boolean.TRUE;
            LOG.info("Media file scanning is disabled");
        }//w  w w  . j ava  2 s. co m
        return;
    } else {
        messageDisabledMediaFiles = Boolean.FALSE;
    }

    int maxResults = configService.getIntProperty("yamj3.scheduler.mediafilescan.maxResults", 20);
    List<QueueDTO> queueElements = mediaStorageService.getMediaFileQueueForScanning(maxResults);
    if (CollectionUtils.isEmpty(queueElements)) {
        LOG.debug("No media files found to scan");
        return;
    }

    LOG.info("Found {} media files to process; scan with {} threads", queueElements.size(), maxThreads);
    BlockingQueue<QueueDTO> queue = new LinkedBlockingQueue<QueueDTO>(queueElements);

    ExecutorService executor = Executors.newFixedThreadPool(maxThreads);
    for (int i = 0; i < maxThreads; i++) {
        MediaInfoRunner worker = new MediaInfoRunner(queue, mediaInfoService);
        executor.execute(worker);
    }
    executor.shutdown();

    // run until all workers have finished
    while (!executor.isTerminated()) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException ignore) {
        }
    }

    LOG.debug("Finished media file scanning");
}

From source file:com.xeiam.xchange.examples.mtgox.v2.service.trade.streaming.MtGoxWebSocketTradeDemo.java

public void start() throws ExecutionException, InterruptedException {

    // Use the default MtGox settings
    Exchange mtGoxExchange = MtGoxV2ExamplesUtils.createExchange();

    ExchangeStreamingConfiguration exchangeStreamingConfiguration = new MtGoxStreamingConfiguration(10, 10000,
            60000, false, null);/* ww w  . j  a  v a 2s .co m*/

    // Interested in the public streaming market data feed (no authentication)
    StreamingExchangeService streamingExchangeService = mtGoxExchange
            .getStreamingExchangeService(exchangeStreamingConfiguration);

    // Open the connections to the exchange
    streamingExchangeService.connect();

    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<?> mtGoxMarketDataFuture = executorService
            .submit(new TradeDataRunnable(streamingExchangeService, mtGoxExchange));

    // the thread waits here until the Runnable is done.
    mtGoxMarketDataFuture.get();

    executorService.shutdown();

    // Disconnect and exit
    System.out.println(Thread.currentThread().getName() + ": Disconnecting...");
    streamingExchangeService.disconnect();
}

From source file:io.anserini.IndexerCW09B.java

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

    System.out.println(/*  w  w w  .  j  av a2 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.setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE);
    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);

    for (Path f : discoverWarcFiles(docDir))
        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();
    } finally {
        writer.close();
    }

    return numIndexed;
}

From source file:org.olegz.uuid.TimeBasedUUIDGeneratorTests.java

@Test
public void performanceTestAsynch() throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(100);
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();/*from   ww  w  .j  ava 2s  . co m*/
    for (int i = 0; i < 1000000; i++) {
        executor.execute(new Runnable() {
            public void run() {
                TimeBasedUUIDGenerator.generateId();
            }
        });
    }
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    stopWatch.stop();
    System.out.println("Generated 1000000 UUID (async) via TimeBasedUUIDGenerator.generateId(): in "
            + stopWatch.getTotalTimeSeconds() + " seconds");

    executor = Executors.newFixedThreadPool(100);
    stopWatch = new StopWatch();
    stopWatch.start();
    for (int i = 0; i < 1000000; i++) {
        executor.execute(new Runnable() {
            public void run() {
                UUID.randomUUID();
            }
        });
    }
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    stopWatch.stop();
    System.out.println("Generated 1000000 UUID (async) via UUID.randomUUID(): in "
            + stopWatch.getTotalTimeSeconds() + " seconds");
}

From source file:org.yamj.core.service.ScanningScheduler.java

@Scheduled(initialDelay = 5000, fixedDelay = 45000)
public void scanMediaData() throws Exception {
    int maxThreads = configService.getIntProperty("yamj3.scheduler.mediadatascan.maxThreads", 1);
    if (maxThreads <= 0) {
        if (!messageDisabledMediaData) {
            messageDisabledMediaData = Boolean.TRUE;
            LOG.info("Media data scanning is disabled");
        }// w ww  . j  a v a 2  s .  c  om
        return;
    } else {
        messageDisabledMediaData = Boolean.FALSE;
    }

    int maxResults = configService.getIntProperty("yamj3.scheduler.mediadatascan.maxResults", 20);
    List<QueueDTO> queueElements = metadataStorageService.getMediaQueueForScanning(maxResults);
    if (CollectionUtils.isEmpty(queueElements)) {
        LOG.debug("No media data found to scan");
        return;
    }

    LOG.info("Found {} media data objects to process; scan with {} threads", queueElements.size(), maxThreads);
    BlockingQueue<QueueDTO> queue = new LinkedBlockingQueue<QueueDTO>(queueElements);

    ExecutorService executor = Executors.newFixedThreadPool(maxThreads);
    for (int i = 0; i < maxThreads; i++) {
        PluginMetadataRunner worker = new PluginMetadataRunner(queue, pluginMetadataService);
        executor.execute(worker);
    }
    executor.shutdown();

    // run until all workers have finished
    while (!executor.isTerminated()) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException ignore) {
        }
    }

    LOG.debug("Finished media data scanning");
}

From source file:org.yamj.core.service.ScanningScheduler.java

@Scheduled(initialDelay = 15000, fixedDelay = 45000)
public void scanArtwork() throws Exception {
    int maxThreads = configService.getIntProperty("yamj3.scheduler.artworkscan.maxThreads", 1);
    if (maxThreads <= 0) {
        if (!messageDisabledArtwork) {
            messageDisabledArtwork = Boolean.TRUE;
            LOG.info("Artwork scanning is disabled");
        }/*from w  w w  . j av  a2  s .  c  om*/
        return;
    } else {
        messageDisabledArtwork = Boolean.FALSE;
    }

    int maxResults = configService.getIntProperty("yamj3.scheduler.artworkscan.maxResults", 30);
    List<QueueDTO> queueElements = artworkStorageService.getArtworkQueueForScanning(maxResults);
    if (CollectionUtils.isEmpty(queueElements)) {
        LOG.debug("No artwork found to scan");
        return;
    }

    LOG.info("Found {} artwork objects to process; scan with {} threads", queueElements.size(), maxThreads);
    BlockingQueue<QueueDTO> queue = new LinkedBlockingQueue<QueueDTO>(queueElements);

    ExecutorService executor = Executors.newFixedThreadPool(maxThreads);
    for (int i = 0; i < maxThreads; i++) {
        ArtworkScannerRunner worker = new ArtworkScannerRunner(queue, artworkScannerService);
        executor.execute(worker);
    }
    executor.shutdown();

    // run until all workers have finished
    while (!executor.isTerminated()) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException ignore) {
        }
    }

    LOG.debug("Finished artwork scanning");
}

From source file:org.yamj.core.service.ScanningScheduler.java

@Scheduled(initialDelay = 10000, fixedDelay = 45000)
public void scanPeopleData() throws Exception {
    int maxThreads = configService.getIntProperty("yamj3.scheduler.peoplescan.maxThreads", 1);
    if (maxThreads <= 0) {
        if (!messageDisabledPeople) {
            messageDisabledPeople = Boolean.TRUE;
            LOG.info("People scanning is disabled");
        }/*from  w w  w .j  ava  2  s .c om*/
        return;
    } else {
        messageDisabledPeople = Boolean.FALSE;
    }

    int maxResults = configService.getIntProperty("yamj3.scheduler.peoplescan.maxResults", 50);
    List<QueueDTO> queueElements = metadataStorageService.getPersonQueueForScanning(maxResults);
    if (CollectionUtils.isEmpty(queueElements)) {
        LOG.debug("No people data found to scan");
        return;
    }

    LOG.info("Found {} people objects to process; scan with {} threads", queueElements.size(), maxThreads);
    BlockingQueue<QueueDTO> queue = new LinkedBlockingQueue<QueueDTO>(queueElements);

    ExecutorService executor = Executors.newFixedThreadPool(maxThreads);
    for (int i = 0; i < maxThreads; i++) {
        PluginMetadataRunner worker = new PluginMetadataRunner(queue, pluginMetadataService);
        executor.execute(worker);
    }
    executor.shutdown();

    // run until all workers have finished
    while (!executor.isTerminated()) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException ignore) {
        }

    }

    LOG.debug("Finished people data scanning");
}

From source file:com.espertech.esper.multithread.TestMTIsolation.java

private void tryIsolated(int numThreads, int numLoops) throws Exception {
    Configuration config = SupportConfigFactory.getConfiguration();
    config.getEngineDefaults().getViewResources().setShareViews(false);
    config.addEventType("SupportBean", SupportBean.class);
    EPServiceProvider engine = EPServiceProviderManager.getDefaultProvider(config);
    engine.initialize();//from  w w w  . ja  v a2 s  .  c  om

    // execute
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future future[] = new Future[numThreads];
    ReentrantReadWriteLock sharedStartLock = new ReentrantReadWriteLock();
    sharedStartLock.writeLock().lock();
    for (int i = 0; i < numThreads; i++) {
        future[i] = threadPool.submit(new IsolateUnisolateCallable(i, engine, numLoops));
    }
    Thread.sleep(100);
    sharedStartLock.writeLock().unlock();

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

    for (int i = 0; i < numThreads; i++) {
        assertTrue((Boolean) future[i].get());
    }
}

From source file:com.iselect.kernal.geo.service.GeographicServiceImpl.java

@Override
public List<Future> importGeos(List<CountryDto> countries) {
    ExecutorService pools = Executors.newFixedThreadPool(2);
    List<Future> results = new ArrayList<>(countries.size());
    for (CountryDto country : countries) {
        Future result = pools.submit(new GeographicCallable(country));
        results.add(result);/*from w w w  .j a va  2  s. co  m*/
    }
    try {
        pools.awaitTermination(1, TimeUnit.MINUTES);
        pools.shutdown();
    } catch (InterruptedException ex) {
        Logger.getLogger(GeographicServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    return results;
}