Example usage for java.util.concurrent ExecutorService invokeAll

List of usage examples for java.util.concurrent ExecutorService invokeAll

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService invokeAll.

Prototype

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException;

Source Link

Document

Executes the given tasks, returning a list of Futures holding their status and results when all complete.

Usage

From source file:org.apache.sysml.runtime.compress.cocode.PlanningCoCoder.java

private static List<int[]> getCocodingGroupsBruteForce(List<List<Integer>> bins,
        HashMap<Integer, GroupableColInfo> groupColsInfo, CompressedSizeEstimator estim, int rlen, int k) {
    List<int[]> retGroups = new ArrayList<>();
    try {/* ww  w.j a  v a2s .co m*/
        ExecutorService pool = CommonThreadPool.get(k);
        ArrayList<CocodeTask> tasks = new ArrayList<>();
        for (List<Integer> bin : bins) {
            // building an array of singleton CoCodingGroup
            ArrayList<PlanningCoCodingGroup> sgroups = new ArrayList<>();
            for (Integer col : bin)
                sgroups.add(new PlanningCoCodingGroup(col, groupColsInfo.get(col)));
            tasks.add(new CocodeTask(estim, sgroups, rlen));
        }
        List<Future<PlanningCoCodingGroup[]>> rtask = pool.invokeAll(tasks);
        for (Future<PlanningCoCodingGroup[]> lrtask : rtask)
            for (PlanningCoCodingGroup grp : lrtask.get())
                retGroups.add(grp.getColIndices());
        pool.shutdown();
    } catch (Exception ex) {
        throw new DMLRuntimeException(ex);
    }

    return retGroups;
}

From source file:org.keycloak.testsuite.saml.ConcurrentAuthnRequestTest.java

private static void loginRepeatedly(UserRepresentation user, URI samlEndpoint, Document samlRequest,
        String relayState, Binding requestBinding) {
    CloseableHttpResponse response = null;
    SamlClient.RedirectStrategyWithSwitchableFollowRedirect strategy = new SamlClient.RedirectStrategyWithSwitchableFollowRedirect();
    ExecutorService threadPool = Executors.newFixedThreadPool(CONCURRENT_THREADS);

    try (CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(strategy).build()) {
        HttpUriRequest post = requestBinding.createSamlUnsignedRequest(samlEndpoint, relayState, samlRequest);

        Collection<Callable<Void>> futures = new LinkedList<>();
        for (int i = 0; i < ITERATIONS; i++) {
            final int j = i;
            Callable<Void> f = () -> {
                performLogin(post, samlEndpoint, relayState, samlRequest, response, client, user, strategy);
                return null;
            };//from ww w  .  j av a  2 s . c o m
            futures.add(f);
        }

        threadPool.invokeAll(futures);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.offbynull.portmapper.upnpigd.UpnpIgdDiscovery.java

private static Map<UpnpIgdDevice, byte[]> getRootXmlForEachDevice(Set<UpnpIgdDevice> devices)
        throws InterruptedException {
    Map<UpnpIgdDevice, byte[]> serviceRoots = new HashMap();

    ExecutorService executorService = null;
    try {//from  w  w w .j av  a2  s . c  o  m
        int maximumPoolSize = (int) ((double) Runtime.getRuntime().availableProcessors() / (1.0 - 0.95));
        executorService = new ThreadPoolExecutor(0, maximumPoolSize, 1, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>());

        List<HttpRequestCallable<UpnpIgdDevice>> tasks = new LinkedList<>();
        for (UpnpIgdDevice device : devices) {
            tasks.add(new HttpRequestCallable<>(device.getUrl(), device));
        }

        List<Future<Pair<UpnpIgdDevice, byte[]>>> results = executorService.invokeAll(tasks);

        for (Future<Pair<UpnpIgdDevice, byte[]>> result : results) {
            try {
                Pair<UpnpIgdDevice, byte[]> data = result.get();
                serviceRoots.put(data.getKey(), data.getValue());
            } catch (InterruptedException | ExecutionException | CancellationException e) { // NOPMD
                // do nothing, skip
            }
        }
    } finally {
        if (executorService != null) {
            executorService.shutdownNow();
        }
    }

    return serviceRoots;
}

From source file:com.offbynull.portmapper.upnpigd.UpnpIgdDiscovery.java

private static Map<UpnpIgdServiceReference, byte[]> getServiceDescriptions(
        Set<UpnpIgdServiceReference> services) throws InterruptedException {
    Map<UpnpIgdServiceReference, byte[]> serviceXmls = new HashMap();

    ExecutorService executorService = null;
    try {/* w  w w.  j a  va2  s .  c  o  m*/
        int maximumPoolSize = (int) ((double) Runtime.getRuntime().availableProcessors() / (1.0 - 0.95));
        executorService = new ThreadPoolExecutor(0, maximumPoolSize, 1, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>());

        List<HttpRequestCallable<UpnpIgdServiceReference>> tasks = new LinkedList<>();
        for (UpnpIgdServiceReference service : services) {
            tasks.add(new HttpRequestCallable<>(service.getScpdUrl(), service));
        }

        List<Future<Pair<UpnpIgdServiceReference, byte[]>>> results = executorService.invokeAll(tasks);

        for (Future<Pair<UpnpIgdServiceReference, byte[]>> result : results) {
            try {
                Pair<UpnpIgdServiceReference, byte[]> data = result.get();
                serviceXmls.put(data.getKey(), data.getValue());
            } catch (InterruptedException | ExecutionException | CancellationException e) { // NOPMD
                // do nothing, skip
            }
        }
    } finally {
        if (executorService != null) {
            executorService.shutdownNow();
        }
    }

    return serviceXmls;
}

From source file:com.newlandframework.avatarmq.core.AckMessageCache.java

public void parallelDispatch(LinkedList<String> list) {
    List<Callable<Long>> tasks = new ArrayList<Callable<Long>>();
    List<Future<Long>> futureList = new ArrayList<Future<Long>>();
    int startPosition = 0;
    Pair<Integer, Integer> pair = calculateBlocks(list.size(), list.size());
    int numberOfThreads = pair.getRight();
    int blocks = pair.getLeft();

    barrier = new CyclicBarrier(numberOfThreads);

    for (int i = 0; i < numberOfThreads; i++) {
        String[] task = new String[blocks];
        System.arraycopy(list.toArray(), startPosition, task, 0, blocks);
        tasks.add(new AckMessageTask(barrier, task));
        startPosition += blocks;//  ww  w  .  j  av  a2 s.c o  m
    }

    ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
    try {
        futureList = executor.invokeAll(tasks);
    } catch (InterruptedException ex) {
        Logger.getLogger(AckMessageCache.class.getName()).log(Level.SEVERE, null, ex);
    }

    for (Future<Long> longFuture : futureList) {
        try {
            succTaskCount += longFuture.get();
        } catch (InterruptedException ex) {
            Logger.getLogger(AckMessageCache.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExecutionException ex) {
            Logger.getLogger(AckMessageCache.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.google.code.guice.repository.testing.junit.general.UserRepositoryTest.java

@Test
public void testRepo() throws Exception {
    userRepository.deleteAll();/*  w w  w. j av  a2 s.  c  om*/
    userRepository.someCustomMethod(new User("one", "two", 42));

    userRepository.deleteInactiveUsers();
    userRepository.deleteOtherUsers();

    userRepository.deleteAll();
    assertEquals("Invalid repository size", 0, userRepository.count());

    userRepository.save(new User("john", "smith", 42));
    userRepository.save(new User("alex", "johns", 33));
    userRepository.save(new User("sam", "brown", 22));
    assertEquals("Invalid repository size", 3, userRepository.count());

    assertNotNull("User not found", userRepository.findUserByName("john"));

    Page<User> users = userRepository.findAll(new PageRequest(0, 100));
    assertEquals("Invalid requested page size", 3, users.getNumberOfElements());

    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.invokeAll(Arrays.asList(Executors.callable(new Runnable() {
        @Override
        public void run() {
            try {
                logger.info("Start concurrent thread");
                UserRepository anotherRepo = userRepository;
                logger.info("count");
                assertEquals("Invalid repository size", 3, anotherRepo.count());
                logger.info("save");
                anotherRepo.save(new User(UUID.randomUUID().toString(), UUID.randomUUID().toString(), 10));
                assertEquals("Invalid repository size", 4, anotherRepo.count());
                logger.info("Stored 4");
            } catch (Exception e) {
                logger.error("Error", e);
            }
        }
    })));

    logger.info("After");
    assertEquals("Invalid repository size", 4, userRepository.count());
    userRepository.deleteAll();
    assertEquals("Invalid repository size", 0, userRepository.count());

    userRepository.someCustomMethod(new User("john", "smith", 42));
}

From source file:org.dasein.cloud.utils.requester.DaseinParallelRequestExecutor.java

public List<T> execute() throws DaseinRequestException {
    final HttpClientBuilder clientBuilder = setProxyIfRequired(httpClientBuilder);

    final CloseableHttpClient httpClient = clientBuilder.build();

    List<T> results = new ArrayList<T>();
    List<Callable<T>> tasks = new ArrayList<Callable<T>>();
    for (final HttpUriRequest httpUriRequest : httpUriRequests) {
        tasks.add(new Callable<T>() {
            @Override//  www  . j  a  v a2  s  .  c o  m
            public T call() throws Exception {
                return execute(httpClient, httpUriRequest);
            }
        });
    }

    try {
        try {
            ExecutorService executorService = Executors.newFixedThreadPool(httpUriRequests.size());
            List<Future<T>> futures = executorService.invokeAll(tasks);
            for (Future<T> future : futures) {
                T result = future.get();
                results.add(result);
            }
            return results;
        } finally {
            httpClient.close();
        }
    } catch (Exception e) {
        throw new DaseinRequestException(e.getMessage(), e);
    }
}

From source file:com.networknt.limit.LimitHandlerTest.java

@Test
public void testMoreRequests() throws Exception {
    Callable<String> task = this::callApi;
    List<Callable<String>> tasks = Collections.nCopies(10, task);
    long start = System.currentTimeMillis();
    ExecutorService executorService = Executors.newFixedThreadPool(10);
    List<Future<String>> futures = executorService.invokeAll(tasks);
    List<String> resultList = new ArrayList<>(futures.size());
    // Check for exceptions
    for (Future<String> future : futures) {
        // Throws an exception if an exception was thrown by the task.
        resultList.add(future.get());/*from   ww w  .  j  av a  2  s  . c  o m*/
    }
    long last = (System.currentTimeMillis() - start);
    // make sure that there are at least one element in resultList is :513
    Assert.assertTrue(resultList.contains(":513"));
    System.out.println("resultList = " + resultList + " response time = " + last);
}

From source file:com.cloudera.oryx.ml.serving.als.LoadIT.java

@Test
public void testRecommendLoad() throws Exception {
    // Since latency is more important, and local machine will also be busy handling requests,
    // use few concurrent workers, like 1:
    int workers = 1;
    AtomicLong count = new AtomicLong();
    Mean meanReqTimeMS = new Mean();
    long start = System.currentTimeMillis();

    List<Callable<Void>> tasks = new ArrayList<>(workers);
    for (int i = 0; i < workers; i++) {
        tasks.add(new LoadCallable(Integer.toString(i), meanReqTimeMS, count, start));
    }//www  . ja  va2  s  .  co m

    ExecutorService executor = Executors.newFixedThreadPool(workers);
    try {
        executor.invokeAll(tasks);
    } finally {
        executor.shutdown();
    }

    int totalRequests = workers * REQS_PER_WORKER;
    log(totalRequests, meanReqTimeMS, start);

    Assert.assertTrue(meanReqTimeMS.getResult() < 300.0);
}

From source file:org.seasar.robot.interval.impl.HostIntervalControllerTest.java

/**
 * ????????????/* ww w .  j a va  2  s.c o m*/
 */
public void test_delayBeforeProcessing() {
    // ?
    final int numTasks = 100;
    // ?
    final Long waittime = 100L;

    CrawlingParameterUtil.setUrlQueue(new UrlQueueImpl());
    final UrlQueue q = CrawlingParameterUtil.getUrlQueue();
    for (int i = 0; i < numTasks; i++) {
        q.setUrl("http://example.com");
    }

    final HostIntervalController controller = new HostIntervalController();
    controller.delayMillisBeforeProcessing = waittime;
    controller.delayMillisAfterProcessing = 0L;
    controller.delayMillisForWaitingNewUrl = 0L;
    controller.delayMillisAtNoUrlInQueue = 0L;

    final Callable<Integer> testCallable = new Callable<Integer>() {
        public Integer call() throws Exception {
            CrawlingParameterUtil.setUrlQueue(q);
            controller.delayBeforeProcessing();
            return 0;
        }
    };

    // Callable?
    final List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
    for (int i = 0; i < numTasks; i++) {
        tasks.add(testCallable);
    }

    // ?
    final StopWatch watch = new StopWatch();
    watch.start();

    // Callable()?
    final ExecutorService executor = Executors.newFixedThreadPool(numTasks);
    try {
        final List<Future<Integer>> futures = executor.invokeAll(tasks);
        for (final Future<Integer> future : futures) {
            future.get();
        }
    } catch (final InterruptedException e) {
        // no thing to do
    } catch (final ExecutionException e) {
        // no thing to do
    }

    assertTrue(watch.getTime() > waittime * (numTasks - 1));
}