package futures; import java.util.concurrent.*; public class FibExec { final static ExecutorService exec = Executors.newFixedThreadPool(400); static int fcall(int n) throws Exception { if (n < 2) return n; Future f1 = exec.submit(() -> fcall(n - 1)); int n2 = fcall(n - 2); return f1.get() + n2; } public static void main(String[] args) throws Exception { for (int n = 0; n <= 45; n++) { final int fibn = n; Future fib = exec.submit(() -> fcall(fibn)); long t1 = System.currentTimeMillis(); int f = fib.get(); long t2 = System.currentTimeMillis(); double tm = 1e-3*(t2-t1); System.out.printf("fib(%d)=%d, time=%f%n", n, f, tm); } exec.shutdown(); } }