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:com.espertech.esper.example.marketdatafeed.FeedSimMain.java

public void run() {
    if (isWaitKeypress) {
        System.out.println("...press enter to start simulation...");
        try {/*from  w w  w .ja va 2s.  c  o  m*/
            System.in.read();
        } catch (IOException e) {
            log.error("Exception reading keyboard input: " + e.getMessage(), e);
        }
    }

    // Configure engine with event names to make the statements more readable.
    // This could also be done in a configuration file.
    Configuration configuration = new Configuration();
    configuration.addEventType("MarketDataEvent", MarketDataEvent.class.getName());

    // Get engine instance
    EPServiceProvider epService = EPServiceProviderManager.getProvider(engineURI, configuration);

    // Set up statements
    TicksPerSecondStatement tickPerSecStmt = new TicksPerSecondStatement(epService.getEPAdministrator());
    tickPerSecStmt.addListener(new RateReportingListener());

    TicksFalloffStatement falloffStmt = new TicksFalloffStatement(epService.getEPAdministrator());
    falloffStmt.addListener(new RateFalloffAlertListener());

    // For continuous non-ending simulation
    if (continuousSimulation) {
        new MarketDataSendRunnable(epService, true).run();
    } else {
        // Send events
        ExecutorService threadPool = Executors.newFixedThreadPool(numberOfThreads);
        MarketDataSendRunnable runnables[] = new MarketDataSendRunnable[numberOfThreads];
        for (int i = 0; i < numberOfThreads; i++) {
            runnables[i] = new MarketDataSendRunnable(epService, false);
            threadPool.submit(runnables[i]);
        }

        int seconds = 0;
        Random random = new Random();
        while (seconds < numSeconds) {
            seconds++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                log.info("Interrupted", e);
                break;
            }

            FeedEnum feedToDropOff;
            if (random.nextDouble() * 100 < dropProbability) {
                feedToDropOff = FeedEnum.FEED_A;
                if (random.nextBoolean()) {
                    feedToDropOff = FeedEnum.FEED_B;
                }
                log.info("Setting drop-off for feed " + feedToDropOff);

            } else {
                feedToDropOff = null;
            }
            for (int i = 0; i < runnables.length; i++) {
                runnables[i].setRateDropOffFeed(feedToDropOff);
            }
        }

        log.info("Shutting down threadpool");
        for (int i = 0; i < runnables.length; i++) {
            runnables[i].setShutdown();
        }
        threadPool.shutdown();
        try {
            threadPool.awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            // no action
        }
    }
}

From source file:org.yccheok.jstock.gui.IndicatorScannerJPanel.java

private void initAlertDataStructures() {
    AlertStateManager oldAlertStateManager = alertStateManager;
    if (oldAlertStateManager != null) {
        oldAlertStateManager.dettachAll();
        oldAlertStateManager.clearState();
    }//from ww w . ja v  a 2  s. c o  m

    final ExecutorService oldSystemTrayAlertPool = systemTrayAlertPool;
    final ExecutorService oldEmailAlertPool = emailAlertPool;

    Utils.getZoombiePool().execute(new Runnable() {
        @Override
        public void run() {
            if (oldSystemTrayAlertPool != null) {
                log.info("Prepare to shut down " + oldSystemTrayAlertPool + "...");
                oldSystemTrayAlertPool.shutdownNow();
                try {
                    oldSystemTrayAlertPool.awaitTermination(100, TimeUnit.DAYS);
                } catch (InterruptedException exp) {
                    log.error(null, exp);
                }
                log.info("Shut down " + oldSystemTrayAlertPool + " peacefully.");

                log.info("Prepare to shut down " + oldEmailAlertPool + "...");
            }

            if (oldEmailAlertPool != null) {
                oldEmailAlertPool.shutdownNow();
                try {
                    oldEmailAlertPool.awaitTermination(100, TimeUnit.DAYS);
                } catch (InterruptedException exp) {
                    log.error(null, exp);
                }
                log.info("Shut down " + oldEmailAlertPool + " peacefully.");
            }
        }
    });

    alertStateManager = new AlertStateManager();
    alertStateManager.attach(this);

    emailAlertPool = Executors.newFixedThreadPool(1);
    systemTrayAlertPool = Executors.newFixedThreadPool(1);
}

From source file:com.alibaba.cobar.client.CobarSqlMapClientTemplate.java

private ExecutorService createExecutorForSpecificDataSource(CobarDataSourceDescriptor descriptor) {
    final String identity = descriptor.getIdentity();
    final ExecutorService executor = createCustomExecutorService(descriptor.getPoolSize(),
            "createExecutorForSpecificDataSource-" + identity + " data source");
    // 1. register executor for disposing explicitly
    internalExecutorServiceRegistry.add(executor);
    // 2. dispose executor implicitly
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override//from  w ww .  ja va 2  s . c  o  m
        public void run() {
            if (executor == null) {
                return;
            }

            try {
                executor.shutdown();
                executor.awaitTermination(5, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                logger.warn("interrupted when shuting down the query executor:\n{}", e);
            }
        }
    });
    return executor;
}

From source file:MSUmpire.LCMSPeakStructure.LCMSPeakDIAMS2.java

private void PrepareMGF_UnfragmentIon() throws IOException {
    String mgffile4 = FilenameUtils.getFullPath(ParentmzXMLName) + GetQ3Name() + ".mgf.temp";
    //        FileWriter mgfWriter4 = new FileWriter(mgffile4, true);
    final BufferedWriter mgfWriter4 = DIAPack.get_file(DIAPack.OutputFile.Mgf_Q3, mgffile4);

    //        FileWriter mapwriter3 = new FileWriter(FilenameUtils.getFullPath(ParentmzXMLName) + FilenameUtils.getBaseName(ParentmzXMLName) + ".ScanClusterMapping_Q3", true);
    final BufferedWriter mapwriter3 = DIAPack.get_file(DIAPack.OutputFile.ScanClusterMapping_Q3,
            FilenameUtils.getFullPath(ParentmzXMLName) + FilenameUtils.getBaseName(ParentmzXMLName)
                    + ".ScanClusterMapping_Q3");

    ArrayList<PseudoMSMSProcessing> ScanList = new ArrayList<>();
    ExecutorService executorPool = Executors.newFixedThreadPool(NoCPUs);
    for (PeakCluster ms2cluster : PeakClusters) {
        ArrayList<PrecursorFragmentPairEdge> frags = UnFragIonClu2Cur.get(ms2cluster.Index);
        if (frags != null && DIA_MZ_Range.getX() <= ms2cluster.TargetMz()
                && DIA_MZ_Range.getY() >= ms2cluster.TargetMz()) {
            //            if (DIA_MZ_Range.getX() <= ms2cluster.TargetMz() && DIA_MZ_Range.getY() >= ms2cluster.TargetMz() && UnFragIonClu2Cur.containsKey(ms2cluster.Index)) {
            //                ArrayList<PrecursorFragmentPairEdge> frags = UnFragIonClu2Cur.get(ms2cluster.Index);
            ms2cluster.GroupedFragmentPeaks.addAll(frags);
            PseudoMSMSProcessing mSMSProcessing = new PseudoMSMSProcessing(ms2cluster, parameter);
            executorPool.execute(mSMSProcessing);
            ScanList.add(mSMSProcessing);
        }//from w ww.j  a v  a 2s  .c  o m
    }
    executorPool.shutdown();
    try {
        executorPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException e) {
        Logger.getRootLogger().info("interrupted..");
    }

    for (PseudoMSMSProcessing mSMSProcessing : ScanList) {
        if (MatchedFragmentMap.size() > 0) {
            mSMSProcessing.RemoveMatchedFrag(MatchedFragmentMap);
        }
        XYPointCollection Scan = mSMSProcessing.GetScan();

        if (Scan != null && Scan.PointCount() > parameter.MinFrag) {
            parentDIA.Q3Scan++;
            //                StringBuilder mgfString = new StringBuilder();
            //                mgfString.append("BEGIN IONS\n");
            //                mgfString.append("PEPMASS=" + mSMSProcessing.Precursorcluster.TargetMz() + "\n");
            //                mgfString.append("CHARGE=" + mSMSProcessing.Precursorcluster.Charge + "+\n");
            //                mgfString.append("RTINSECONDS=" + mSMSProcessing.Precursorcluster.PeakHeightRT[0] * 60f + "\n");
            //                mgfString.append("TITLE=").append(GetQ3Name()).append(".").append(parentDIA.Q3Scan).append(".").append(parentDIA.Q3Scan).append(".").append(mSMSProcessing.Precursorcluster.Charge).append("\n");
            //                //mgfString.append("TITLE=" + WindowID + ";ClusterIndex:" + mSMSProcessing.ms2cluster.Index + "\n");
            //                //mgfString.append("TITLE=" GetQ3Name() + WindowID + ";ClusterIndex:" + mSMSProcessing.ms2cluster.Index + "\n");
            //
            //                for (int i = 0; i < Scan.PointCount(); i++) {
            //                    mgfString.append(Scan.Data.get(i).getX()).append(" ").append(Scan.Data.get(i).getY()).append("\n");
            //                }
            //                mgfString.append("END IONS\n\n");
            //                mgfWriter4.write(mgfString.toString());

            mgfWriter4.append("BEGIN IONS\n")
                    .append("PEPMASS=" + mSMSProcessing.Precursorcluster.TargetMz() + "\n")
                    .append("CHARGE=" + mSMSProcessing.Precursorcluster.Charge + "+\n")
                    .append("RTINSECONDS=" + mSMSProcessing.Precursorcluster.PeakHeightRT[0] * 60f + "\n")
                    .append("TITLE=").append(GetQ3Name()).append(".").append(Integer.toString(parentDIA.Q3Scan))
                    .append(".").append(Integer.toString(parentDIA.Q3Scan)).append(".")
                    .append(Integer.toString(mSMSProcessing.Precursorcluster.Charge)).append("\n");
            //mgfWriter4.append("TITLE=" + WindowID + ";ClusterIndex:" + mSMSProcessing.ms2cluster.Index + "\n");
            //mgfWriter4.append("TITLE=" GetQ3Name() + WindowID + ";ClusterIndex:" + mSMSProcessing.ms2cluster.Index + "\n");

            for (int i = 0; i < Scan.PointCount(); i++) {
                mgfWriter4.append(Float.toString(Scan.Data.get(i).getX())).append(" ")
                        .append(Float.toString(Scan.Data.get(i).getY())).append("\n");
            }
            mgfWriter4.append("END IONS\n\n");

            mapwriter3.write(
                    parentDIA.Q3Scan + ";" + WindowID + ";" + mSMSProcessing.Precursorcluster.Index + "\n");
        }
        mSMSProcessing.Precursorcluster.GroupedFragmentPeaks.clear();
    }
    //        mgfWriter4.close();
    //        mapwriter3.close();
}

From source file:org.lilyproject.repository.impl.AbstractSchemaCache.java

public void start() throws InterruptedException, KeeperException, RepositoryException {
    cacheRefresher.start();/*from   w ww . j a  va  2  s  .  c om*/

    ZkUtil.createPath(zooKeeper, CACHE_INVALIDATION_PATH);
    final ExecutorService threadPool = Executors.newFixedThreadPool(50);
    final List<Future> futures = new ArrayList<Future>();
    for (int i = 0; i < 16; i++) {
        final int index = i;
        futures.add(threadPool.submit(new Callable<Void>() {
            @Override
            public Void call() throws InterruptedException, KeeperException, RepositoryException {
                for (int j = 0; j < 16; j++) {
                    String bucket = "" + DIGITS_LOWER[index] + DIGITS_LOWER[j];
                    ZkUtil.createPath(zooKeeper, bucketPath(bucket));
                    cacheWatchers.add(new CacheWatcher(bucket));
                }

                return null;
            }
        }));
    }
    for (Future future : futures) {
        try {
            future.get();
        } catch (ExecutionException e) {
            throw new RuntimeException("failed to start cache", e);
        }
    }
    threadPool.shutdown();
    threadPool.awaitTermination(1, TimeUnit.HOURS);
    ZkUtil.createPath(zooKeeper, CACHE_REFRESHENABLED_PATH);
    connectionWatcher = new ConnectionWatcher();
    zooKeeper.addDefaultWatcher(connectionWatcher);
    readRefreshingEnabledState();
    refreshAll();
}

From source file:edu.lternet.pasta.dml.util.DocumentDownloadUtil.java

public InputStream downloadDocument(final String id, final EcogridEndPointInterface endpoint) throws Exception {

    init();/*from ww w  .  jav  a 2  s.  c  o m*/

    log.debug("starting the download");

    ExecutorService service = Executors.newSingleThreadExecutor();
    service.execute(new Runnable() {
        public void run() {
            long startTime = System.currentTimeMillis();

            try {
                if (endpoint instanceof AuthenticatedEcogridEndPointInterface) {
                    AuthenticatedQueryServiceGetToStreamClient authenticatedEcogridClient = new AuthenticatedQueryServiceGetToStreamClient(
                            new URL(((AuthenticatedEcogridEndPointInterface) endpoint)
                                    .getMetacatAuthenticatedEcogridEndPoint()));
                    authenticatedEcogridClient.get(id,
                            ((AuthenticatedEcogridEndPointInterface) endpoint).getSessionId(), outputStream);
                } else {
                    QueryServiceGetToStreamClient ecogridClient = new QueryServiceGetToStreamClient(
                            new URL(endpoint.getMetacatEcogridEndPoint()));
                    ecogridClient.get(id, outputStream);
                }
                outputStream.close();

                long endTime = System.currentTimeMillis();
                log.debug((endTime - startTime) + " ms to download document data");

                log.debug("Done downloading id=" + id);

            } catch (Exception e) {
                log.error("Error getting document from ecogrid: " + e.getMessage());
                e.printStackTrace();
            }

        }
    });

    //wait for the download to complete
    service.shutdown();
    service.awaitTermination(0, TimeUnit.SECONDS);

    log.debug("done with the download");

    return inputStream;

}

From source file:com.blacklocus.qs.worker.util.log.SamplingQSLogServiceTest.java

@Test
public void testSampledLifeCycle() throws InterruptedException {

    // With these parameters, by far most logger interactions should be filtered out, very few sampled in.
    final int numThreads = 64, iterations = 200, processingJitterMaxMs = 16, noSoonerThanMs = 100;

    final Set<String> sampledTaskIds = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());

    final QSLogService logService = Mockito.mock(QSLogService.class);
    // track which logging interactions were allowed through (sampled in)
    Mockito.doAnswer(new Answer() {
        @Override//from   w  w  w.  j  av a2 s.c  om
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sampledTaskIds.add(((QSTaskModel) invocation.getArguments()[0]).taskId);
            return null;
        }
    }).when(logService).startedTask(Matchers.any(QSTaskModel.class));
    Mockito.doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sampledTaskIds.add(((QSLogModel) invocation.getArguments()[0]).taskId);
            return null; //TODO jason
        }
    }).when(logService).log(Matchers.any(QSLogModel.class));
    Mockito.doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sampledTaskIds.add(((QSTaskModel) invocation.getArguments()[0]).taskId);
            return null; //TODO jason
        }
    }).when(logService).completedTask(Matchers.any(QSTaskModel.class));

    Predicate<QSTaskModel> taskPredicate = SamplingPredicates.noSoonerThan(noSoonerThanMs,
            TimeUnit.MILLISECONDS);
    final QSLogService sampledLogService = new SamplingQSLogService(logService, taskPredicate);

    long startNs = System.nanoTime();
    ExecutorService threads = Executors.newFixedThreadPool(numThreads);
    for (int i = 0; i < numThreads; i++) {
        threads.submit(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                LOG.debug("Thread start {}", Thread.currentThread().getName());
                for (int i = 0; i < iterations; i++) {

                    String taskId = UUID.randomUUID().toString();

                    // simulate task processing, some have logs, some don't, processing time varies between each step
                    QSTaskModel task = new QSTaskModel();
                    task.taskId = taskId;
                    Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs));

                    sampledLogService.startedTask(task);
                    Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs));

                    // random number of associated logs [0, 2]
                    for (int j = RandomUtils.nextInt(2); j > 0; j--) {
                        QSLogModel log = new QSLogModel();
                        log.taskId = taskId;
                        sampledLogService.log(log);
                        Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs));
                    }

                    sampledLogService.completedTask(task);
                }
                LOG.debug("Thread end {}", Thread.currentThread().getName());
                return null;

            }
        });
    }
    threads.shutdown();
    threads.awaitTermination(1, TimeUnit.MINUTES);
    long endNs = System.nanoTime();

    // Theoretical maximum number of sampled in task logging
    long durationMs = TimeUnit.NANOSECONDS.toMillis(endNs - startNs);
    long expectedMax = durationMs / noSoonerThanMs + 1; // +1 for time@0: sampled in
    LOG.debug("Run duration: {}ms  no sooner than: {}ms", durationMs, noSoonerThanMs);
    LOG.debug("Expected max sampled in: {}  Actually sampled: {}", expectedMax, sampledTaskIds.size());
    Assert.assertTrue(expectedMax >= sampledTaskIds.size());
}

From source file:org.apache.bookkeeper.tools.perf.dlog.PerfWriter.java

void execute(Namespace namespace) throws Exception {
    List<Pair<Integer, DistributedLogManager>> managers = new ArrayList<>(flags.numLogs);
    for (int i = 0; i < flags.numLogs; i++) {
        String logName = String.format(flags.logName, i);
        managers.add(Pair.of(i, namespace.openLog(logName)));
    }/*w w  w.j  a  va2 s  .  c o m*/
    log.info("Successfully open {} logs", managers.size());

    // register shutdown hook to aggregate stats
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        isDone.set(true);
        printAggregatedStats(cumulativeRecorder);
    }));

    ExecutorService executor = Executors.newFixedThreadPool(flags.numThreads);
    try {
        for (int i = 0; i < flags.numThreads; i++) {
            final int idx = i;
            final List<DistributedLogManager> logsThisThread = managers.stream()
                    .filter(pair -> pair.getLeft() % flags.numThreads == idx).map(pair -> pair.getRight())
                    .collect(Collectors.toList());
            final long numRecordsForThisThread = flags.numRecords / flags.numThreads;
            final long numBytesForThisThread = flags.numBytes / flags.numThreads;
            final double writeRateForThisThread = flags.writeRate / (double) flags.numThreads;
            final long maxOutstandingBytesForThisThread = flags.maxOutstandingMB * 1024 * 1024
                    / flags.numThreads;
            executor.submit(() -> {
                try {
                    write(logsThisThread, writeRateForThisThread, (int) maxOutstandingBytesForThisThread,
                            numRecordsForThisThread, numBytesForThisThread);
                } catch (Exception e) {
                    log.error("Encountered error at writing records", e);
                }
            });
        }
        log.info("Started {} write threads", flags.numThreads);
        reportStats();
    } finally {
        executor.shutdown();
        if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
            executor.shutdownNow();
        }
        managers.forEach(manager -> manager.getRight().asyncClose());
    }
}

From source file:org.eclipse.rdf4j.http.server.ProtocolTest.java

/**
 * Test for SES-1861//from   ww  w .j a  va  2  s.c om
 * 
 * @throws Exception
 */
@Test
public void testConcurrentNamespaceUpdates() throws Exception {
    int limitCount = 1000;
    int limitPrefix = 50;

    Random prng = new Random();

    // String repositoryLocation =
    // Protocol.getRepositoryLocation("http://localhost:8080/openrdf-sesame",
    // "Test-NativeStore");
    String repositoryLocation = TestServer.REPOSITORY_URL;

    ExecutorService threadPool = Executors.newFixedThreadPool(20,
            new ThreadFactoryBuilder().setNameFormat("rdf4j-protocoltest-%d").build());

    for (int count = 0; count < limitCount; count++) {
        final int number = count;
        final int i = prng.nextInt(limitPrefix);
        final String prefix = "prefix" + i;
        final String ns = "http://example.org/namespace" + i;

        final String location = Protocol.getNamespacePrefixLocation(repositoryLocation, prefix);

        Runnable runner = new Runnable() {

            public void run() {
                try {
                    if (number % 2 == 0) {
                        putNamespace(location, ns);
                    } else {
                        deleteNamespace(location);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    fail("Failed in test: " + number);
                }
            }
        };
        threadPool.execute(runner);
    }
    threadPool.shutdown();
    threadPool.awaitTermination(30000, TimeUnit.MILLISECONDS);
    threadPool.shutdownNow();
}

From source file:org.apache.nifi.controller.scheduling.TestStandardProcessScheduler.java

/**
 * Validates that in multi threaded environment enabling service can still
 * be disabled. This test is set up in such way that disabling of the
 * service could be initiated by both disable and enable methods. In other
 * words it tests two conditions in//from   w w  w .  j  a v a  2 s . com
 * {@link StandardControllerServiceNode#disable(java.util.concurrent.ScheduledExecutorService, Heartbeater)}
 * where the disabling of the service can be initiated right there (if
 * ENABLED), or if service is still enabling its disabling will be deferred
 * to the logic in
 * {@link StandardControllerServiceNode#enable(java.util.concurrent.ScheduledExecutorService, long, Heartbeater)}
 * IN any even the resulting state of the service is DISABLED
 */
@Test
@Ignore
public void validateEnabledDisableMultiThread() throws Exception {
    final ProcessScheduler scheduler = createScheduler();
    final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller,
            scheduler, null, stateMgrProvider, variableRegistry, nifiProperties);
    final ExecutorService executor = Executors.newCachedThreadPool();
    for (int i = 0; i < 200; i++) {
        final ControllerServiceNode serviceNode = provider.createControllerService(
                RandomShortDelayEnablingService.class.getName(), "1",
                systemBundle.getBundleDetails().getCoordinate(), null, false);

        executor.execute(new Runnable() {
            @Override
            public void run() {
                scheduler.enableControllerService(serviceNode);
            }
        });
        Thread.sleep(10); // ensure that enable gets initiated before disable
        executor.execute(new Runnable() {
            @Override
            public void run() {
                scheduler.disableControllerService(serviceNode);
            }
        });
        Thread.sleep(100);
        assertFalse(serviceNode.isActive());
        assertTrue(serviceNode.getState() == ControllerServiceState.DISABLED);
    }

    // need to sleep a while since we are emulating async invocations on
    // method that is also internally async
    Thread.sleep(500);
    executor.shutdown();
    executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
}