Java File Attribute parallel(ExecutorService executor, Collection> tasks)

Here you can find the source of parallel(ExecutorService executor, Collection> tasks)

Description

parallel

License

Apache License

Declaration

private static void parallel(ExecutorService executor, Collection<? extends Callable<?>> tasks)
            throws IOException, InterruptedException 

Method Source Code

//package com.java2s;
/**/* w  w w .j a v  a2 s  .c o  m*/
 * Copyright 2011-2017 Asakusa Framework Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import java.util.Collection;

import java.util.List;

import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

import java.util.concurrent.Future;

import java.util.stream.Collectors;

public class Main {
    private static void parallel(ExecutorService executor, Collection<? extends Callable<?>> tasks)
            throws IOException, InterruptedException {
        List<Future<?>> futures = tasks.stream().map(task -> executor.submit(task)).collect(Collectors.toList());
        for (Future<?> future : futures) {
            try {
                future.get();
            } catch (CancellationException | InterruptedException e) {
                cancel(futures);
                throw e;
            } catch (ExecutionException e) {
                cancel(futures);
                try {
                    throw e.getCause();
                } catch (Error | RuntimeException | IOException | InterruptedException cause) {
                    throw cause;
                } catch (Throwable cause) {
                    throw new IOException(cause);
                }
            }
        }
    }

    private static void cancel(List<? extends Future<?>> futures) {
        futures.forEach(f -> f.cancel(true));
    }
}

Related

  1. makeExecutable(File file)
  2. makeExecutable(File file)
  3. makeExecutable(File file)
  4. makeExecutable(File target)
  5. makeExecutable(String path)
  6. patchInfoPList(final File infoPList, final String executable)
  7. persistExecutionTimesCsv(String filePath, LinkedList> executionTimes)
  8. setExecutable(File f, final String pattern)
  9. setExecutable(File file)