Created
November 28, 2017 23:16
-
-
Save xhiroga/ff0ecbf615aba39f628dff32ff4b40a7 to your computer and use it in GitHub Desktop.
Java5で実装されたExecutorServiceを、newCachedThreadPool()で作って利用するプログラムです。書き方が悪いのか、簡単にOOMEになります...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.concurrent.*; | |
public class Fib5 { | |
private ExecutorService exec = Executors.newCachedThreadPool(); | |
public class Task implements Callable<Integer> { | |
private final int n; | |
public Task(int n){ | |
this.n = n; | |
} | |
@Override | |
public Integer call() throws Exception { | |
if(n <= 1){ | |
return n; | |
} | |
Future<Integer> f1 = exec.submit(new Task(n-1)); | |
Future<Integer> f2 = exec.submit(new Task(n-2)); | |
return f1.get() + f2.get(); | |
} | |
} | |
public void doExecute(){ | |
try{ | |
Future<Integer> f = exec.submit(new Task(10)); | |
System.out.println(f.get()); | |
}catch (Exception e){ | |
System.out.println(e); | |
}finally{ | |
exec.shutdown(); | |
} | |
} | |
public static void main(String args[]){ | |
Fib5 f = new Fib5(); | |
f.doExecute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment