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.stimulus.archiva.incoming.IAPRunnable.java

public void shutdownAndAwaitTermination(ExecutorService pool, String job) {
    pool.shutdown();//from  www .  j a  va 2s  .  c o m
    try {
        logger.debug("awaiting termination of " + job);
        if (!pool.awaitTermination(DEAD_PERIOD, TimeUnit.MILLISECONDS)) {
            logger.debug("awaiting " + job + " did not terminate");
            pool.shutdownNow();
            if (!pool.awaitTermination(60, TimeUnit.SECONDS))
                logger.debug("awaiting " + job + " still did not terminate");
        } else {
            logger.debug("awaiting " + job + " terminated");
        }
    } catch (InterruptedException ie) {
        logger.debug("awaiting " + job + " were interrupted. shutting thread pool down immediately.");
        pool.shutdownNow();
        Thread.currentThread().interrupt();
    } catch (Throwable e) {
        logger.debug("awaiting " + job + " were interrupted:" + e.getMessage());
        pool.shutdownNow();
        Thread.currentThread().interrupt();
    }
}

From source file:org.apache.carbondata.core.reader.CarbonDeleteFilesDataReader.java

/**
 * Returns all deleted records from all specified delta files
 *
 * @param deltaFiles/*from   www  .j  a v  a 2  s  .c  o m*/
 * @return
 * @throws Exception
 */
public int[] getDeleteDataFromAllFiles(List<String> deltaFiles, String blockletId) throws Exception {

    List<Future<DeleteDeltaBlockDetails>> taskSubmitList = new ArrayList<>();
    ExecutorService executorService = Executors.newFixedThreadPool(thread_pool_size);
    for (final String deltaFile : deltaFiles) {
        taskSubmitList.add(executorService.submit(new Callable<DeleteDeltaBlockDetails>() {
            @Override
            public DeleteDeltaBlockDetails call() throws IOException {
                CarbonDeleteDeltaFileReaderImpl deltaFileReader = new CarbonDeleteDeltaFileReaderImpl(deltaFile,
                        FileFactory.getFileType(deltaFile));
                return deltaFileReader.readJson();
            }
        }));
    }
    try {
        executorService.shutdown();
        executorService.awaitTermination(30, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        LOGGER.error("Error while reading the delete delta files : " + e.getMessage());
    }

    Set<Integer> result = new TreeSet<Integer>();
    for (int i = 0; i < taskSubmitList.size(); i++) {
        try {
            List<DeleteDeltaBlockletDetails> blockletDetails = taskSubmitList.get(i).get().getBlockletDetails();
            result.addAll(blockletDetails
                    .get(blockletDetails.indexOf(new DeleteDeltaBlockletDetails(blockletId))).getDeletedRows());
        } catch (Throwable e) {
            LOGGER.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }
    return ArrayUtils.toPrimitive(result.toArray(new Integer[result.size()]));

}

From source file:org.mule.module.mongo.tools.MongoDump.java

public void dump(final String outputDirectory, final String database, String outputName, final int threads)
        throws IOException {
    Validate.notNull(outputDirectory);//from  w w  w.java 2s .  c o  m
    Validate.notNull(outputName);
    Validate.notNull(database);

    outputName += appendTimestamp();

    initOplog(database);

    final Collection<String> collections = mongoClient.listCollections();
    if (collections != null) {
        final ExecutorService executor = Executors.newFixedThreadPool(threads);
        final DumpWriter dumpWriter = new BsonDumpWriter(outputDirectory, outputName);
        for (final String collectionName : collections) {
            final DBCollection dbCollection = mongoClient.getCollection(collectionName);
            final MongoDumpCollection dumpCollection = new MongoDumpCollection(dbCollection);
            dumpCollection.setDumpWriter(dumpWriter);

            final Future<Void> future = executor.submit(dumpCollection);
            propagateException(future);
        }

        executor.shutdown();
        try {
            if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
                executor.shutdownNow();
            }

            if (oplog) {
                final ExecutorService singleExecutor = Executors.newSingleThreadExecutor();
                final MongoDumpCollection dumpCollection = new MongoDumpCollection(oplogCollection);
                dumpCollection.setName(BackupConstants.OPLOG);
                dumpCollection.addOption(Bytes.QUERYOPTION_OPLOGREPLAY);
                dumpCollection.addOption(Bytes.QUERYOPTION_SLAVEOK);
                final DBObject query = new BasicDBObject();
                query.put(BackupConstants.TIMESTAMP_FIELD, new BasicDBObject("$gt", oplogStart));
                // Filter only oplogs for given database
                query.put(BackupConstants.NAMESPACE_FIELD, BackupUtils.getNamespacePattern(database));
                dumpCollection.setQuery(query);
                dumpCollection.setDumpWriter(dumpWriter);
                final Future<Void> future = singleExecutor.submit(dumpCollection);
                propagateException(future);
            }

            if (zip) {
                final String dbDumpPath = outputDirectory + File.separator + outputName;
                ZipUtils.zipDirectory(dbDumpPath);
                FileUtils.deleteDirectory(new File(dbDumpPath));
            }
        } catch (final InterruptedException ie) {
            executor.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }
}

From source file:hivemall.mix.server.MixServerTest.java

@Test
public void test2ClientsZeroOneDenseModelWithMixCanceling() throws InterruptedException {
    final int port = NetUtils.getAvailablePort();
    CommandLine cl = CommandLineUtils.parseOptions(
            new String[] { "-port", Integer.toString(port), "-sync_threshold", "30" }, MixServer.getOptions());
    MixServer server = new MixServer(cl);
    ExecutorService serverExec = Executors.newSingleThreadExecutor();
    serverExec.submit(server);//from   w  w w .  j ava  2  s  . c  om

    waitForState(server, ServerState.RUNNING);

    final ExecutorService clientsExec = Executors.newCachedThreadPool();
    for (int i = 0; i < 2; i++) {
        clientsExec.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    invokeClient01("test2ClientsZeroOne", port, true, true);
                } catch (InterruptedException e) {
                    Assert.fail(e.getMessage());
                }
            }
        });
    }
    clientsExec.awaitTermination(30, TimeUnit.SECONDS);
    clientsExec.shutdown();
    serverExec.shutdown();
}

From source file:hivemall.mix.server.MixServerTest.java

@Test
public void test2ClientsZeroOneDenseModel() throws InterruptedException {
    final int port = NetUtils.getAvailablePort();
    CommandLine cl = CommandLineUtils.parseOptions(
            new String[] { "-port", Integer.toString(port), "-sync_threshold", "30" }, MixServer.getOptions());
    MixServer server = new MixServer(cl);
    ExecutorService serverExec = Executors.newSingleThreadExecutor();
    serverExec.submit(server);//from   ww w  .j a v a  2s . com

    waitForState(server, ServerState.RUNNING);

    final ExecutorService clientsExec = Executors.newCachedThreadPool();
    for (int i = 0; i < 2; i++) {
        clientsExec.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    invokeClient01("test2ClientsZeroOne", port, true, false);
                } catch (InterruptedException e) {
                    Assert.fail(e.getMessage());
                }
            }
        });
    }
    clientsExec.awaitTermination(30, TimeUnit.SECONDS);
    clientsExec.shutdown();
    serverExec.shutdown();
}

From source file:hivemall.mix.server.MixServerTest.java

@Test
public void test2ClientsZeroOneSparseModelWithMixCanceling() throws InterruptedException {
    final int port = NetUtils.getAvailablePort();
    CommandLine cl = CommandLineUtils.parseOptions(
            new String[] { "-port", Integer.toString(port), "-sync_threshold", "30" }, MixServer.getOptions());
    MixServer server = new MixServer(cl);
    ExecutorService serverExec = Executors.newSingleThreadExecutor();
    serverExec.submit(server);/*from   w  w w .ja  v  a  2  s .c  o m*/

    waitForState(server, ServerState.RUNNING);

    final ExecutorService clientsExec = Executors.newCachedThreadPool();
    for (int i = 0; i < 2; i++) {
        clientsExec.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    invokeClient01("test2ClientsZeroOne", port, false, true);
                } catch (InterruptedException e) {
                    Assert.fail(e.getMessage());
                }
            }
        });
    }
    clientsExec.awaitTermination(30, TimeUnit.SECONDS);
    clientsExec.shutdown();
    serverExec.shutdown();
}

From source file:hivemall.mix.server.MixServerTest.java

@Test
public void test2ClientsZeroOneSparseModel() throws InterruptedException {
    final int port = NetUtils.getAvailablePort();
    CommandLine cl = CommandLineUtils.parseOptions(
            new String[] { "-port", Integer.toString(port), "-sync_threshold", "30" }, MixServer.getOptions());
    MixServer server = new MixServer(cl);
    ExecutorService serverExec = Executors.newSingleThreadExecutor();
    serverExec.submit(server);/*  ww  w  .j  a va  2s .  com*/

    waitForState(server, ServerState.RUNNING);

    final ExecutorService clientsExec = Executors.newCachedThreadPool();
    for (int i = 0; i < 2; i++) {
        clientsExec.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    invokeClient01("test2ClientsZeroOne", port, false, false);
                } catch (InterruptedException e) {
                    Assert.fail(e.getMessage());
                }
            }
        });
    }
    clientsExec.awaitTermination(30, TimeUnit.SECONDS);
    clientsExec.shutdown();
    serverExec.shutdown();
}

From source file:org.apache.carbondata.core.reader.CarbonDeleteFilesDataReader.java

/**
 * returns delete delta file details for the specified block name
 * @param deltaFiles/*from  ww w.ja  v  a  2 s . c  o  m*/
 * @param blockName
 * @return DeleteDeltaBlockDetails
 * @throws Exception
 */
public DeleteDeltaBlockDetails getCompactedDeleteDeltaFileFromBlock(List<String> deltaFiles, String blockName)
        throws Exception {
    // get the data.
    List<Future<DeleteDeltaBlockDetails>> taskSubmitList = new ArrayList<>(deltaFiles.size());
    ExecutorService executorService = Executors.newFixedThreadPool(thread_pool_size);
    for (final String deltaFile : deltaFiles) {
        taskSubmitList.add(executorService.submit(new Callable<DeleteDeltaBlockDetails>() {
            @Override
            public DeleteDeltaBlockDetails call() throws IOException {
                CarbonDeleteDeltaFileReaderImpl deltaFileReader = new CarbonDeleteDeltaFileReaderImpl(deltaFile,
                        FileFactory.getFileType(deltaFile));
                return deltaFileReader.readJson();
            }
        }));
    }
    try {
        executorService.shutdown();
        executorService.awaitTermination(30, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        LOGGER.error("Error while reading the delete delta files : " + e.getMessage());
    }

    // Get a new DeleteDeltaBlockDetails as result set where all the data will me merged
    // based on each Blocklet.
    DeleteDeltaBlockDetails deleteDeltaResultSet = new DeleteDeltaBlockDetails(blockName);

    for (int i = 0; i < taskSubmitList.size(); i++) {
        try {
            List<DeleteDeltaBlockletDetails> blockletDetails = taskSubmitList.get(i).get().getBlockletDetails();
            for (DeleteDeltaBlockletDetails blocklet : blockletDetails) {
                deleteDeltaResultSet.addBlockletDetails(blocklet);
            }
        } catch (Throwable e) {
            LOGGER.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }
    return deleteDeltaResultSet;
}

From source file:werecloud.api.bean.VMWareMacCache.java

private void initializeMACCache() throws RemoteException, InterruptedException {
    long start = System.currentTimeMillis();
    ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
    ExecutorService exec = Executors.newFixedThreadPool(loadThreads);
    try {//from  w  ww  .j  a  v a  2 s .co  m
        for (final ManagedEntity me : mes) {
            exec.submit(new Runnable() {
                @Override
                public void run() {
                    VirtualMachine vm = (VirtualMachine) me;
                    addVirtualMachine(vm);
                }
            });
        }
    } finally {
        exec.shutdown();
    }
    exec.awaitTermination(1, TimeUnit.HOURS);
    logger.info("{} MAC addresses added and took {} milliseconds.", macAddresses.size(),
            System.currentTimeMillis() - start);
}

From source file:org.apache.carbondata.processing.loading.sort.impl.UnsafeParallelReadMergeSorterWithColumnRangeImpl.java

@Override
public Iterator<CarbonRowBatch>[] sort(Iterator<CarbonRowBatch>[] iterators) throws CarbonDataLoadingException {
    UnsafeSortDataRows[] sortDataRows = new UnsafeSortDataRows[columnRangeInfo.getNumOfRanges()];
    intermediateFileMergers = new UnsafeIntermediateMerger[columnRangeInfo.getNumOfRanges()];
    SortParameters[] sortParameterArray = new SortParameters[columnRangeInfo.getNumOfRanges()];
    try {/* w  w  w  .ja v a  2  s .co  m*/
        for (int i = 0; i < columnRangeInfo.getNumOfRanges(); i++) {
            SortParameters parameters = originSortParameters.getCopy();
            parameters.setPartitionID(i + "");
            parameters.setRangeId(i);
            sortParameterArray[i] = parameters;
            setTempLocation(parameters);
            intermediateFileMergers[i] = new UnsafeIntermediateMerger(parameters);
            sortDataRows[i] = new UnsafeSortDataRows(parameters, intermediateFileMergers[i],
                    inMemoryChunkSizeInMB);
            sortDataRows[i].initialize();
        }
    } catch (Exception e) {
        throw new CarbonDataLoadingException(e);
    }
    ExecutorService executorService = Executors.newFixedThreadPool(iterators.length);
    this.threadStatusObserver = new ThreadStatusObserver(executorService);
    final int batchSize = CarbonProperties.getInstance().getBatchSize();
    try {
        for (int i = 0; i < iterators.length; i++) {
            executorService.execute(new SortIteratorThread(iterators[i], sortDataRows, rowCounter,
                    this.insideRowCounterList, this.threadStatusObserver));
        }
        executorService.shutdown();
        executorService.awaitTermination(2, TimeUnit.DAYS);
        processRowToNextStep(sortDataRows, originSortParameters);
    } catch (Exception e) {
        checkError();
        throw new CarbonDataLoadingException("Problem while shutdown the server ", e);
    }
    checkError();
    try {
        for (int i = 0; i < intermediateFileMergers.length; i++) {
            intermediateFileMergers[i].finish();
        }
    } catch (Exception e) {
        throw new CarbonDataLoadingException(e);
    }

    Iterator<CarbonRowBatch>[] batchIterator = new Iterator[columnRangeInfo.getNumOfRanges()];
    for (int i = 0; i < sortDataRows.length; i++) {
        batchIterator[i] = new MergedDataIterator(sortParameterArray[i], batchSize, intermediateFileMergers[i]);
    }

    return batchIterator;
}