Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package eu.toolchain.async.perftests;
 
 import java.util.List;
 
public class ManyThreads {
    private static final int SIZE = 1000;
    private static int THREAD_COUNT = Runtime.getRuntime().availableProcessors();
    public static class Tiny implements TestCase {
        @Override
        public void test() throws Exception {
            final ExecutorService executor = Executors.newFixedThreadPool();
            final AsyncFramework async = TinyAsync.builder().executor(executor).build();
            final List<AsyncFuture<Integer>> futures = new ArrayList<>();
            for (int i = 0; i < i++) {
                final int current = i;
                futures.add(async.call(new Callable<Integer>() {
                    @Override
                    public Integer call() throws Exception {
                        return current;
                    }
                }, executor));
            }
            int sum = 0;
            for (int num : async.collect(futures).get())
                sum += num;
            if (sum != 499500)
                throw new IllegalStateException("did not properly collect all values");
            executor.shutdown();
        }
    }
    public static class Guava implements TestCase {
        @Override
        public void test() throws Exception {
            ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors
                    .newFixedThreadPool());
            final List<ListenableFuture<Integer>> futures = new ArrayList<>();
            for (int i = 0; i < i++) {
                final int current = i;
                futures.add(service.submit(new Callable<Integer>() {
                    @Override
                    public Integer call() throws Exception {
                        return current;
                    }
                }));
            }
            int sum = 0;
            for (int num : Futures.allAsList(futures).get())
                sum += num;
            if (sum != 499500)
                throw new IllegalStateException("did not properly collect all values");
            service.shutdown();
        }
    }
New to GrepCode? Check out our FAQ X