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.deegree.services.wms.TileLayerPerformanceTest.java

@Test
public void testPerformance() throws IOException, InterruptedException, OWSExceptionReport, XMLStreamException {
    String base = "http://localhost:" + System.getProperty("portnumber");
    base += "/deegree-wms-tiling-tests/services";
    WMSClient client = new WMSClient(new URL(base + "?request=GetCapabilities&service=WMS&version=1.1.1"));

    // skip test if layer is not available, then we probably don't have the huge file available
    Assume.assumeTrue(client.hasLayer("performance"));

    String crs = client.getCoordinateSystems("performance").getFirst();

    Envelope envelope = client.getBoundingBox(crs, "performance");
    double minx = envelope.getMin().get0();
    double miny = envelope.getMin().get1();
    double res = 0.14;
    double spanx = res * 800;
    double spany = res * 600;
    base += "?request=GetMap&service=WMS&version=1.1.1&layers=performance&styles=&width=800&height=600&";
    base += "format=image/png&transparent=true&srs=" + crs + "&bbox=";

    List<Callable<Object>> list = new ArrayList<Callable<Object>>();

    for (int i = 0; i < 100; ++i) {
        String url = base + minx + "," + miny + ",";
        minx += spanx;//from  w  ww.  ja  va  2 s .co m
        miny += spany;
        url += minx + "," + miny;
        list.add(new Fetcher(url));
    }

    ExecutorService service = Executors.newFixedThreadPool(10);

    long t1 = System.currentTimeMillis();
    service.invokeAll(list);
    t1 = System.currentTimeMillis() - t1;
    System.out.println("Requested 100 images, 10 in parallel, took " + (t1 / 1000) + " seconds.");
    double avg = (t1 / 100d) / 1000d;
    System.out.println("Average secs/request: " + avg);
    Assert.assertTrue("Average response time was too high.", avg < 5);
}

From source file:org.silverpeas.core.silverstatistics.volume.service.DirectoryVolumeService.java

long getTotalSize() throws InterruptedException, ExecutionException {
    List<DirectorySizeComputer> scanners = buildSizeScanners(workspace, null);
    long totalSize = 0L;
    ExecutorService executor = Executors.newFixedThreadPool(getNumberOfThread());
    List<Future<DirectoryStats>> result = executor.invokeAll(scanners);
    try {//from  ww w.  j a  v  a2  s. c om
        for (Future<DirectoryStats> future : result) {
            DirectoryStats stats = future.get();
            totalSize = totalSize + stats.getDirectorySize();
        }
    } finally {
        executor.shutdown();
    }
    return totalSize;
}

From source file:org.silverpeas.core.silverstatistics.volume.service.DirectoryVolumeService.java

public Map<String, String[]> getSizeVentilation(String userId) throws InterruptedException, ExecutionException {
    List<DirectorySizeComputer> scanners = buildSizeScanners(workspace, userId);
    Map<String, String[]> volume = new HashMap<>(scanners.size());
    ExecutorService executor = Executors.newFixedThreadPool(getNumberOfThread());
    List<Future<DirectoryStats>> result = executor.invokeAll(scanners);
    try {//from   w  w  w .  ja v  a 2s.c  om
        for (Future<DirectoryStats> future : result) {
            DirectoryStats stats = future.get();
            volume.put(stats.getDirectoryName(),
                    new String[] { String.valueOf(stats.getDirectorySize()), null, null });
        }
    } finally {
        executor.shutdown();
    }
    return volume;
}

From source file:org.silverpeas.core.silverstatistics.volume.service.DirectoryVolumeService.java

public Map<String, String[]> getFileNumberVentilation(String userId)
        throws InterruptedException, ExecutionException {
    List<FileNumberComputer> scanners = buildFileNumberScanners(workspace, userId);
    Map<String, String[]> volume = new HashMap<>(scanners.size());
    ExecutorService executor = Executors.newFixedThreadPool(getNumberOfThread());
    List<Future<DirectoryStats>> result = executor.invokeAll(scanners);
    try {/*  w  w  w.  j a va2  s  .c  o  m*/
        for (Future<DirectoryStats> future : result) {
            DirectoryStats stats = future.get();
            volume.put(stats.getDirectoryName(),
                    new String[] { String.valueOf(stats.getNumberOfFiles()), null, null });
        }
    } finally {
        executor.shutdown();
    }
    return volume;
}

From source file:org.wso2.carbon.la.core.utils.LogPatternExtractor.java

public void processAll(int noOfThreads, int chunkSize) throws Exception {
    int count = (int) ((file.length() + chunkSize - 1) / chunkSize);
    java.util.List<Callable<Integer>> tasks = new ArrayList<>(count);
    for (int i = 0; i < count; i++)
        tasks.add(processPartTask(i * chunkSize, Math.min(file.length(), (i + 1) * chunkSize)));
    ExecutorService es = Executors.newFixedThreadPool(noOfThreads);

    java.util.List<Future<Integer>> results = es.invokeAll(tasks);
    es.shutdown();/*from  w  ww . ja va  2  s  .c om*/

    for (Future<Integer> result : results)
        System.out.println(result.get());
}

From source file:org.silverpeas.silverstatistics.volume.DirectoryVolumeService.java

public List<DirectoryStats> getVolumes(String userId) throws InterruptedException, ExecutionException {
    List<DirectoryWalkerSizeComputer> scanners = buildScanners(workspace, userId);
    List<DirectoryStats> volume = new ArrayList<DirectoryStats>(scanners.size());
    ExecutorService executor = Executors.newFixedThreadPool(getNumberOfThread());
    List<Future<DirectoryStats>> result = executor.invokeAll(scanners);
    try {/*from ww  w .  j  av a2  s.  co  m*/
        for (Future<DirectoryStats> future : result) {
            volume.add(future.get());
        }
    } finally {
        executor.shutdown();
    }
    return volume;
}

From source file:org.silverpeas.silverstatistics.volume.DirectoryVolumeService.java

public long getTotalSize(String userId) throws InterruptedException, ExecutionException {
    List<DirectorySizeComputer> scanners = buildSizeScanners(workspace, userId);
    long totalSize = 0L;
    ExecutorService executor = Executors.newFixedThreadPool(getNumberOfThread());
    List<Future<DirectoryStats>> result = executor.invokeAll(scanners);
    try {/*www.  j  a  v a 2  s  . c o m*/
        for (Future<DirectoryStats> future : result) {
            DirectoryStats stats = future.get();
            totalSize = totalSize + stats.getDirectorySize();
        }
    } finally {
        executor.shutdown();
    }
    return totalSize;
}

From source file:org.silverpeas.silverstatistics.volume.DirectoryVolumeService.java

public Map<String, String[]> getSizeVentilation(String userId) throws InterruptedException, ExecutionException {
    List<DirectorySizeComputer> scanners = buildSizeScanners(workspace, userId);
    Map<String, String[]> volume = new HashMap<String, String[]>(scanners.size());
    ExecutorService executor = Executors.newFixedThreadPool(getNumberOfThread());
    List<Future<DirectoryStats>> result = executor.invokeAll(scanners);
    try {/*from w ww.j  av a  2s .  c  o m*/
        for (Future<DirectoryStats> future : result) {
            DirectoryStats stats = future.get();
            volume.put(stats.getDirectoryName(),
                    new String[] { String.valueOf(stats.getDirectorySize()), null, null });
        }
    } finally {
        executor.shutdown();
    }
    return volume;
}

From source file:org.silverpeas.silverstatistics.volume.DirectoryVolumeService.java

public Map<String, String[]> getFileNumberVentilation(String userId)
        throws InterruptedException, ExecutionException {
    List<FileNumberComputer> scanners = buildFileNumberScanners(workspace, userId);
    Map<String, String[]> volume = new HashMap<String, String[]>(scanners.size());
    ExecutorService executor = Executors.newFixedThreadPool(getNumberOfThread());
    List<Future<DirectoryStats>> result = executor.invokeAll(scanners);
    try {//  w w w. ja va  2s .c o  m
        for (Future<DirectoryStats> future : result) {
            DirectoryStats stats = future.get();
            volume.put(stats.getDirectoryName(),
                    new String[] { String.valueOf(stats.getNumberOfFiles()), null, null });
        }
    } finally {
        executor.shutdown();
    }
    return volume;
}

From source file:com.qpark.eip.core.spring.lockedoperation.EipTest.java

/**
 * Test to run several synchronous {@link LockableOperation}s in parallel.
 *///from w ww.j  a  va 2  s .  c o  m
@Test
public void testLockableOperationTestSyncCall() {
    this.logger.debug("+testLockableOperationTestSyncCall");
    int threadCount = 4;

    OperationEventEnumType start = OperationEventEnumType.START;
    LockableOperationContext context = new LockableOperationContext();
    List<OperationStateEnumType> status = new ArrayList<>();
    Callable<Void> task = () -> {
        OperationStateEnumType value = EipTest.this.operationSync
                .runOperation(EipTest.this.operationSync.getUUID(), start, context);
        status.add(value);
        this.logger.debug(" testLockableOperationTestSyncCall returned {}", value);
        return null;
    };
    List<Callable<Void>> tasks = Collections.nCopies(threadCount, task);
    ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
    List<Future<Void>> futures;
    try {
        futures = executorService.invokeAll(tasks);
        List<Void> resultList = new ArrayList<>(futures.size());
        // Check for exceptions
        for (Future<Void> future : futures) {
            // Throws an exception if an exception was thrown by the task.
            resultList.add(future.get());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    int runnings = status.stream().filter(s -> s.equals(OperationStateEnumType.RUNNING))
            .collect(Collectors.toList()).size();
    int idle = status.stream().filter(s -> s.equals(OperationStateEnumType.IDLE)).collect(Collectors.toList())
            .size();
    Assert.assertEquals("No the right number of sync proccesses got the RUNNING return.", runnings,
            threadCount - 1);
    Assert.assertEquals("To many IDLE sync processes", idle, 1);
    OperationStateEnumType idleResult = this.operationSync.runOperation(this.operationSync.getUUID(),
            OperationEventEnumType.CHECK_STATE, context);
    Assert.assertEquals("Cleanup missing at sync processes", idleResult, OperationStateEnumType.IDLE);
    this.logger.debug("+testLockableOperationTestSyncCall");
}