Example usage for java.util.concurrent CompletionService submit

List of usage examples for java.util.concurrent CompletionService submit

Introduction

In this page you can find the example usage for java.util.concurrent CompletionService submit.

Prototype

Future<V> submit(Callable<V> task);

Source Link

Document

Submits a value-returning task for execution and returns a Future representing the pending results of the task.

Usage

From source file:com.amazon.s3.S3ParserTest.java

@Test
void testAmazonParseListAllMyBucketsParallelResponseTime() throws InterruptedException, ExecutionException {
    CompletionService<Boolean> completer = new ExecutorCompletionService<Boolean>(exec);

    for (int i = 0; i < LOOP_COUNT; i++)
        completer.submit(new Callable<Boolean>() {
            public Boolean call() throws IOException {
                runAmazonParseListAllMyBuckets();
                return true;
            }// ww  w.j av  a 2  s  .c  o  m
        });
    for (int i = 0; i < LOOP_COUNT; i++)
        assert completer.take().get();
}

From source file:com.amazon.s3.S3ParserTest.java

@Test(enabled = false)
void testAmazonParseListBucketResultParallelResponseTime() throws InterruptedException, ExecutionException {
    CompletionService<Boolean> completer = new ExecutorCompletionService<Boolean>(exec);

    for (int i = 0; i < LOOP_COUNT; i++)
        completer.submit(new Callable<Boolean>() {
            public Boolean call() throws IOException {
                runAmazonParseListBucketResult();
                return true;
            }//  w w  w. j  av a  2  s. c o  m
        });
    for (int i = 0; i < LOOP_COUNT; i++)
        assert completer.take().get();
}

From source file:cn.clxy.upload.UploadFileService.java

private void doUpload() {

    listener.onStart(indexes != null ? indexes.size() : getPartCount());
    parts = new ArrayBlockingQueue<Part>(Config.maxRead);
    CompletionService<String> cs = new ExecutorCompletionService<String>(executor);

    cs.submit(readTask);

    for (int i = 0; i < Config.maxUpload; i++) {
        cs.submit(new UploadTask("upload." + i));
    }/*from w  w  w.  j av  a2 s.c  o  m*/

    // Wait all done. total count = maxUpload + 1.
    for (int i = 0; i <= Config.maxUpload; i++) {
        Future<String> future = null;
        try {
            future = cs.take();
            checkFuture(future);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    // Notify sever all done.
    Future<String> result = executor.submit(notifyTask);
    checkFuture(result);
    listener.onSuccess();
}

From source file:org.jclouds.aws.s3.xml.S3ParserTest.java

@Test
void testParseListContainerResultParallelResponseTime() throws InterruptedException, ExecutionException {
    CompletionService<ListBucketResponse> completer = new ExecutorCompletionService<ListBucketResponse>(exec);
    for (int i = 0; i < LOOP_COUNT; i++)
        completer.submit(new Callable<ListBucketResponse>() {
            public ListBucketResponse call() throws IOException, SAXException, HttpException {
                return runParseListContainerResult();
            }//from w ww  .  j a  v a 2s  .  co  m
        });
    for (int i = 0; i < LOOP_COUNT; i++)
        assert completer.take().get() != null;
}

From source file:nl.b3p.viewer.image.ImageManager.java

public void process() throws Exception {
    ExecutorService threadPool = Executors.newFixedThreadPool(MAX_TREADS);
    CompletionService<ImageCollector> pool = new ExecutorCompletionService<ImageCollector>(threadPool);
    for (ImageCollector ic : ics) {
        if (ic.getStatus() == ImageCollector.NEW) {
            pool.submit(ic);
        }// w w  w . j ava  2 s.c om
    }
    //wait for all to complete. Wait max 5 min
    for (int i = 0; i < ics.size(); i++) {
        pool.poll(5, TimeUnit.MINUTES).get();
    }
}

From source file:org.jclouds.aws.s3.xml.S3ParserTest.java

@Test
void testParseListAllMyBucketsParallelResponseTime() throws InterruptedException, ExecutionException {
    CompletionService<SortedSet<BucketMetadata>> completer = new ExecutorCompletionService<SortedSet<BucketMetadata>>(
            exec);//from www .j a v a  2s. c om
    for (int i = 0; i < LOOP_COUNT; i++)
        completer.submit(new Callable<SortedSet<BucketMetadata>>() {
            public SortedSet<BucketMetadata> call() throws IOException, SAXException, HttpException {
                return runParseListAllMyBuckets();
            }
        });
    for (int i = 0; i < LOOP_COUNT; i++)
        assert completer.take().get() != null;
}

From source file:com.flipkart.bifrost.CommunicationTest.java

@Test
public void testSendReceive() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

    Connection connection = new Connection(Lists.newArrayList("localhost"), "guest", "guest");
    connection.start();/*from   w  w w.  j  a v  a2  s .c o m*/

    BifrostExecutor<Void> executor = BifrostExecutor.<Void>builder(TestAction.class).connection(connection)
            .objectMapper(mapper).requestQueue("bifrost-send").responseQueue("bifrost-recv").concurrency(10)
            .executorService(Executors.newFixedThreadPool(10)).build();

    BifrostRemoteCallExecutionServer<Void> executionServer = BifrostRemoteCallExecutionServer
            .<Void>builder(TestAction.class).objectMapper(mapper).connection(connection).concurrency(10)
            .requestQueue("bifrost-send").build();
    executionServer.start();

    long startTime = System.currentTimeMillis();
    AtomicInteger counter = new AtomicInteger(0);
    int requestCount = 100;
    CompletionService<Void> ecs = new ExecutorCompletionService<>(Executors.newFixedThreadPool(50));
    List<Future<Void>> futures = Lists.newArrayListWithCapacity(requestCount);
    for (int i = 0; i < requestCount; i++) {
        futures.add(ecs.submit(new ServiceCaller(executor, counter)));
    }
    for (int i = 0; i < requestCount; i++) {
        try {
            ecs.take().get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    System.out.println(
            String.format("Completed: %d in %d ms", counter.get(), (System.currentTimeMillis() - startTime)));
    executor.shutdown();
    executionServer.stop();
    connection.stop();

    Assert.assertEquals(requestCount, counter.get());
}

From source file:org.codice.ddf.admin.common.PrioritizedBatchExecutor.java

private List<CompletionService<T>> getPrioritizedCompletionServices() {
    List<CompletionService<T>> prioritizedCompletionServices = new ArrayList<>();

    for (List<Callable<T>> taskBatch : tasks) {
        CompletionService<T> completionService = new ExecutorCompletionService<>(threadPool);

        for (Callable<T> task : taskBatch) {
            completionService.submit(task);
        }/* w  ww.java 2  s  .  c  o  m*/

        prioritizedCompletionServices.add(completionService);
    }

    return prioritizedCompletionServices;
}

From source file:org.gitana.platform.load.AbstractLoadTest.java

protected List<RunnerResult<V>> execute() throws Exception {
    ExecutorService executorService = createExecutorService();

    CompletionService<V> cs = new ExecutorCompletionService<V>(executorService);

    for (int i = 0; i < getIterationCount(); i++) {
        // create the runner
        Runner<V> runner = createRunner("runner-" + i);
        runner.init();//w ww  .  j  a v  a  2  s  . c o  m

        cs.submit(runner);
    }

    // wait for everything to finish
    List<RunnerResult<V>> results = new ArrayList<RunnerResult<V>>();
    for (int i = 0; i < getIterationCount(); i++) {
        RunnerResult<V> result = null;

        try {
            V v = cs.take().get();

            result = new RunnerResult<V>(v);
        } catch (Exception ex) {
            ex.printStackTrace();

            result = new RunnerResult<V>();
            result.setException(ex);
        }

        results.add(result);
    }

    return results;
}

From source file:org.apache.hadoop.hbase.util.TestDrainableQueue.java

@Test(timeout = 30 * 1000)
public void testDrainableQueue() throws Exception {
    for (int attempt = 0; attempt < NUM_ATTEMPTS; ++attempt) {
        final int totalEvents = NUM_PRODUCERS * NUM_EVENTS_PER_BATCH;
        final int drainAfterNEvents = totalEvents / 2;
        shouldDrain = new CountDownLatch(drainAfterNEvents);
        numEnqueued.set(0);/*from  w  w  w .  ja v  a  2s . c o  m*/
        q = new DrainableQueue<Integer>("queue");
        ExecutorService exec = Executors.newFixedThreadPool(NUM_PRODUCERS);
        CompletionService<Void> cs = new ExecutorCompletionService<Void>(exec);
        List<Future<Void>> futures = new ArrayList<Future<Void>>();
        for (int producer = 0; producer < NUM_PRODUCERS; ++producer) {
            futures.add(cs.submit(new Producer(producer)));
        }
        shouldDrain.await();
        eventsProcessed = 0;
        LOG.info("Starting draining the queue");
        q.drain(this);
        LOG.info("Finished draining the queue");
        assertEquals(numEnqueued.get(), eventsProcessed);
        LOG.info("Events processed: " + eventsProcessed + ", drainAfterNEvents: " + drainAfterNEvents);
        assertTrue(eventsProcessed >= drainAfterNEvents);
        for (Future<Void> f : futures) {
            try {
                f.get();
            } catch (ExecutionException ex) {
                LOG.error("Exception from producer thread", ex);
                if (ex.getCause() instanceof AssertionError) {
                    throw (AssertionError) ex.getCause();
                }
                throw ex;
            }
        }
        exec.shutdown();
        assertTrue(exec.awaitTermination(5, TimeUnit.SECONDS));
    }
}