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:com.uwsoft.editor.data.manager.DataManager.java

public void importExternalImagesIntoProject(final ArrayList<File> files, ProgressHandler progressHandler) {
    handler = progressHandler;/*from   ww w .j a  v  a2 s  . com*/
    currentPercent = 0;
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(new Runnable() {
        @Override
        public void run() {
            copyImageFilesForAllResolutionsIntoProject(files, true);
            resolutionManager.rePackProjectImagesForAllResolutions();
        }
    });
    executor.execute(new Runnable() {
        @Override
        public void run() {
            changePercentBy(100 - currentPercent);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            handler.progressComplete();
        }
    });
    executor.shutdown();
}

From source file:com.sm.connector.server.ExecMapReduce.java

/**
 * each thread will process 3 of record (end -begin) /noOfThread
 * @param store -name of store/* ww w.  java 2 s .c  o m*/
 * @param noOfThread how many thread to run concurrently
 * @param begin record#  for this node
 * @param end record#  for this node
 * @return by reducer
 */
public Object execute(String store, int noOfThread, int begin, int end) {
    logger.info("execute " + store + " threads " + noOfThread + " begin " + begin + " end " + end);
    if (noOfThread <= 0 || begin >= end)
        throw new RuntimeException(
                "number of thread " + noOfThread + " must be > 0  or begin " + begin + " >= end " + end);
    serverStore = serverStoreMap.get(store);
    if (serverStore == null)
        throw new RuntimeException("can not find ServerStore " + store);
    //how many record need to be process
    int totalRec = end - begin;
    int blockSize = (totalRec % noOfThread == 0 ? totalRec / noOfThread : (totalRec / noOfThread) + 1);
    CountDownLatch countDownLatch = new CountDownLatch(noOfThread);
    ExecutorService executor = Executors.newFixedThreadPool(noOfThread, new ThreadPoolFactory("exec"));
    List<Runnable> runnableList = new ArrayList<Runnable>(noOfThread);
    logger.info("start to run " + noOfThread + " threads block size " + blockSize + " for " + store + " total "
            + totalRec);
    for (int i = 0; i < noOfThread; i++) {
        try {
            //T t = tClass.newInstance();
            T t = (T) QueryUtils.createInstance(tClass);
            if (i < noOfThread - 1) {
                RunThread runThread = new RunThread(countDownLatch, begin + blockSize * i,
                        begin + blockSize * (i + 1), t, i, store);
                runnableList.add(runThread);
                executor.submit(runThread);
            } else { //the last block
                RunThread runThread = new RunThread(countDownLatch, begin + blockSize * i, begin + totalRec, t,
                        i, store);
                runnableList.add(runThread);
                executor.submit(runThread);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    try {
        countDownLatch.await(timeout * 10, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        logger.warn(ex.getMessage(), ex);
    } finally {
        executor.shutdown();
        return mapReduce.reduce(list);
    }

}

From source file:com.sm.store.client.ClusterClient.java

/**
 * nextCursor will continue call to each cluster node to iterate through server side cursor
 * isEnd() return true, mean the end of cursor for each cluster
  * @param clusterCursor//from  ww w  .  j  a v  a  2s .co m
 * @return  ClusterCursor
 */
public ClusterCursor nextCursor(ClusterCursor clusterCursor) {
    //clear out keyValueList first
    clusterCursor.resetCursorParaList();
    if (clusterCursor.isEnd()) {
        logger.info("isEnd true for clusterCursor  size " + clusterCursor.getCursorParaList().size());
        return clusterCursor;
    }
    int size = clusterCursor.getKeyClusterList().size();
    CountDownLatch countDownLatch = new CountDownLatch(size);
    ExecutorService executor = Executors.newFixedThreadPool(size);
    List<Runnable> runnableList = new ArrayList<Runnable>(size);
    for (int i = 0; i < size; i++) {
        //create request for nextCursor
        Request request = createCursorRequest(clusterCursor.getCursorParaList().get(i));
        RunThread runThread = new RunThread(request, clusterCursor.getKeyClusterList().get(i), countDownLatch,
                OpType.Scan);
        runnableList.add(runThread);
        executor.submit(runThread);
    }
    try {
        // add 5 times to handle large package for store proc
        countDownLatch.await(timeout * 5, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        logger.warn(ex.getMessage(), ex);
    } finally {
        executor.shutdown();
        return mergeCluster(runnableList, clusterCursor);
    }
}

From source file:com.easarrive.aws.plugins.common.service.impl.SQSNotificationService.java

/**
 * {@inheritDoc}/*  w w  w .  ja  va 2  s .  c om*/
 */
@Override
public List<NotificationHandleResult<Message, Boolean>> handleNotification(Message message)
        throws AWSPluginException {
    try {
        if (message == null) {
            throw new AWSPluginException("The message is null");
        }
        if (notificationHandlerList == null) {
            throw new AWSPluginException("The notificationHandlerList is null for message (ID : %s)",
                    message.getMessageId());
        }
        if (notificationHandlerList.isEmpty()) {
            throw new AWSPluginException("The notificationHandlerList is empty for message (ID : %s)",
                    message.getMessageId());
        }
        ExecutorService executorService = Executors.newCachedThreadPool();
        List<Future<List<NotificationHandleResult<Message, Boolean>>>> resultList = new ArrayList<Future<List<NotificationHandleResult<Message, Boolean>>>>();
        for (INotificationHandler<Message, Boolean> notificationHandler : notificationHandlerList) {
            Future<List<NotificationHandleResult<Message, Boolean>>> future = executorService
                    .submit(new NotificationHandlerCallable(notificationHandler, message));
            resultList.add(future);
        }

        List<NotificationHandleResult<Message, Boolean>> returnList = new ArrayList<NotificationHandleResult<Message, Boolean>>();
        //??
        for (Future<List<NotificationHandleResult<Message, Boolean>>> fs : resultList) {
            try {
                List<NotificationHandleResult<Message, Boolean>> result = fs.get();
                for (NotificationHandleResult<Message, Boolean> notificationHandleResult : result) {
                    returnList.add(notificationHandleResult);
                }
            } catch (Exception e) {
                if (logger.isErrorEnabled()) {
                    logger.error(e.getMessage(), e);
                }
                returnList.add(
                        new NotificationHandleResult<Message, Boolean>(message.getMessageId(), message, false));
            } finally {
                //????????
                executorService.shutdown();
            }
        }
        return returnList;
    } catch (Exception e) {
        throw new AWSPluginException(e.getMessage(), e);
    }
}

From source file:org.piraso.server.service.ResponseLoggerServiceImplTest.java

@Test
public void testLogging() throws IOException, TransformerConfigurationException, ParserConfigurationException,
        ExecutionException, InterruptedException, SAXException {
    final AtomicBoolean fail = new AtomicBoolean(false);
    ExecutorService executor = Executors.newFixedThreadPool(2);

    final List<MessageEntry> expectedEntries = new ArrayList<MessageEntry>() {
        {/*from ww w.ja va  2 s .  co  m*/
            for (int i = 0; i < 1000; i++) {
                add(new MessageEntry(1l, "test_" + (i + 1)));
            }
        }
    };

    // stop the service when number of entries is reached.
    stopOnWriteTimes(expectedEntries.size());

    Runnable startServiceRunnable = new Runnable() {
        public void run() {
            try {
                service.start();
            } catch (Exception e) {
                fail.set(true);
                e.printStackTrace();
            }
        }
    };

    Runnable logMessagesRunnable = new Runnable() {
        public void run() {
            try {
                // this entry should be ignored since this will throw an exception
                service.log(new ExceptionThrowEntry(1l));

                // these entries should succeed
                for (MessageEntry entry : expectedEntries) {
                    service.log(entry);
                }
            } catch (IOException e) {
                fail.set(true);
                e.printStackTrace();
            }
        }
    };

    Future future = executor.submit(startServiceRunnable);
    executor.submit(logMessagesRunnable);

    future.get();
    executor.shutdown();

    if (fail.get()) {
        fail("failure see exception trace.");
    }

    final List<Entry> entriesRead = new ArrayList<Entry>();
    PirasoEntryReader reader = new PirasoEntryReader(
            new ByteArrayInputStream(response.getContentAsByteArray()));
    reader.addListener(new EntryReadAdapter() {
        @Override
        public void readEntry(EntryReadEvent evt) {
            entriesRead.add(evt.getEntry());
        }
    });

    // start reading
    reader.start();

    assertEquals(service.getId(), reader.getId());
    assertEquals(expectedEntries.size(), entriesRead.size());
}

From source file:com.p2p.peercds.common.Torrent.java

private static String hashFiles(List<File> files) throws InterruptedException, IOException {
    int threads = getHashingThreadsCount();
    ExecutorService executor = Executors.newFixedThreadPool(threads);
    ByteBuffer buffer = ByteBuffer.allocate(PIECE_LENGTH);
    List<Future<String>> results = new LinkedList<Future<String>>();
    StringBuilder hashes = new StringBuilder();

    long length = 0L;
    int pieces = 0;

    long start = System.nanoTime();
    for (File file : files) {
        logger.info("Hashing data from {} with {} threads ({} pieces)...", new Object[] { file.getName(),
                threads, (int) (Math.ceil((double) file.length() / PIECE_LENGTH)) });

        length += file.length();/*from ww w  . ja v  a 2s.  c  om*/

        FileInputStream fis = new FileInputStream(file);
        FileChannel channel = fis.getChannel();
        int step = 10;

        try {
            while (channel.read(buffer) > 0) {
                if (buffer.remaining() == 0) {
                    buffer.clear();
                    results.add(executor.submit(new CallableChunkHasher(buffer)));
                }

                if (results.size() >= threads) {
                    pieces += accumulateHashes(hashes, results);
                }

                if (channel.position() / (double) channel.size() * 100f > step) {
                    logger.info("  ... {}% complete", step);
                    step += 10;
                }
            }
        } finally {
            channel.close();
            fis.close();
        }
    }

    // Hash the last bit, if any
    if (buffer.position() > 0) {
        buffer.limit(buffer.position());
        buffer.position(0);
        results.add(executor.submit(new CallableChunkHasher(buffer)));
    }

    pieces += accumulateHashes(hashes, results);

    // Request orderly executor shutdown and wait for hashing tasks to
    // complete.
    executor.shutdown();
    while (!executor.isTerminated()) {
        Thread.sleep(10);
    }
    long elapsed = System.nanoTime() - start;

    int expectedPieces = (int) (Math.ceil((double) length / PIECE_LENGTH));
    logger.info("Hashed {} file(s) ({} bytes) in {} pieces ({} expected) in {}ms.", new Object[] { files.size(),
            length, pieces, expectedPieces, String.format("%.1f", elapsed / 1e6), });

    return hashes.toString();
}

From source file:com.sm.store.client.ClusterClient.java

public List<String> query4Json(String queryStr) {
    List<KeyCluster> keyClusterList = findAll();
    OpType opType = OpType.QueryStatement;
    int size = keyClusterList.size();
    CountDownLatch countDownLatch = new CountDownLatch(size);
    ExecutorService executor = Executors.newFixedThreadPool(size);
    List<Runnable> runnableList = new ArrayList<Runnable>(size);
    for (int i = 0; i < size; i++) {
        Request request = create4GetPutRequest(keyClusterList.get(i).getKeyList(), opType, queryStr);
        //set SerializableType to json
        request.getHeader().setSerializableType(Header.SerializableType.Json);
        RunThread runThread = new RunThread(request, keyClusterList.get(i), countDownLatch,
                OpType.QueryStatement);/*from ww w .  ja  v a  2s.c om*/
        runnableList.add(runThread);
        executor.submit(runThread);
    }
    try {
        countDownLatch.await(timeout * 5, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        logger.warn(ex.getMessage(), ex);
    } finally {
        executor.shutdown();
        List<String> toReturn = new ArrayList<String>();
        for (Object each : mergeStoreProc(runnableList))
            toReturn.add((String) each);
        return toReturn;
    }

}

From source file:io.undertow.server.handlers.accesslog.AccessLogFileTestCase.java

@Test
public void testLogLotsOfThreads() throws IOException, InterruptedException, ExecutionException {
    Path directory = logDirectory;
    Path logFileName = directory.resolve("server2.log");

    DefaultAccessLogReceiver logReceiver = new DefaultAccessLogReceiver(DefaultServer.getWorker(), directory,
            "server2.");
    CompletionLatchHandler latchHandler;
    DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(NUM_REQUESTS * NUM_THREADS,
            new AccessLogHandler(HELLO_HANDLER, logReceiver, "REQ %{i,test-header}",
                    AccessLogFileTestCase.class.getClassLoader())));

    ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    try {/*from   ww  w. ja va2s  .co  m*/

        final List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < NUM_THREADS; ++i) {
            final int threadNo = i;
            futures.add(executor.submit(new Runnable() {
                @Override
                public void run() {
                    TestHttpClient client = new TestHttpClient();
                    try {
                        for (int i = 0; i < NUM_REQUESTS; ++i) {
                            HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
                            get.addHeader("test-header", "thread-" + threadNo + "-request-" + i);
                            HttpResponse result = client.execute(get);
                            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
                            final String response = HttpClientUtils.readResponse(result);
                            Assert.assertEquals("Hello", response);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        client.getConnectionManager().shutdown();
                    }
                }
            }));
        }
        for (Future<?> future : futures) {
            future.get();
        }

    } finally {
        executor.shutdown();
    }
    latchHandler.await();
    logReceiver.awaitWrittenForTest();
    String completeLog = new String(Files.readAllBytes(logFileName));
    for (int i = 0; i < NUM_THREADS; ++i) {
        for (int j = 0; j < NUM_REQUESTS; ++j) {
            Assert.assertTrue(completeLog.contains("REQ thread-" + i + "-request-" + j));
        }
    }

}

From source file:edu.lternet.pasta.dml.download.DocumentHandler.java

public InputStream downloadDocument() throws Exception {

    log.info("starting the download");

    boolean success = true;

    final String id = docId;
    final EcogridEndPointInterface endpoint = ecogridEndPointInterface;

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

            //get from the ecogrid
            try {
                if (ecogridEndPointInterface 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);
                }/*from  w w w.j av a2 s .  co  m*/
                outputStream.close();
                long endTime = System.currentTimeMillis();
                log.debug((endTime - startTime) + " ms to download: " + docId);
                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);

    return inputStream;
}