Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

import java.util.List;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableFutureTask;

public class Main {
    public static <T, R> ListenableFuture<List<R>> parallelTransform(Collection<T> input,
            final Function<T, R> function) {
        final ExecutorService pool = Executors.newCachedThreadPool();
        return parallelTransform(input, function, pool);
    }

    public static <T, R> ListenableFuture<List<R>> parallelTransform(Collection<T> input,
            final Function<T, R> function, final ExecutorService pool) {

        final List<ListenableFuture<R>> futures = Lists.newLinkedList();
        // make futures
        for (final T in : input) {
            ListenableFutureTask<R> task = ListenableFutureTask.create(new Callable<R>() {
                @Override
                public R call() throws Exception {
                    return function.apply(in);
                }
            });
            pool.submit(task);
            futures.add(task);
        }
        return Futures.successfulAsList(futures);
    }
}