Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Last active August 29, 2015 13:57
Show Gist options
  • Save zaltoprofen/9781879 to your computer and use it in GitHub Desktop.
Save zaltoprofen/9781879 to your computer and use it in GitHub Desktop.
import java.util.concurrent.*;
import java.util.Arrays;
import java.util.stream.Collectors;
public class StreamEtude{
public static void main (String[] args) {
ExecutorService exe = Executors.newFixedThreadPool(4);
Arrays.stream(args)
.map(Integer::parseInt)
.map( arg -> exe.submit(() -> fib(arg)) ).collect(Collectors.toList()).stream()
.forEach( result -> {
try{System.out.println(result.get());}
catch(Exception ex){throw new RuntimeException(ex);}
});
exe.shutdown();
// 上と同じような動作
Arrays.stream(args).parallel().map(Integer::parseInt).map(StreamEtude::fib)
.collect(Collectors.toList()).stream()
.forEach(System.out::println);
}
static int fib(int x){
System.out.println(String.format("Called fib(%d)", x));
return fib_(x);
}
static int fib_(int x){
if (x == 0 || x == 1) return x;
return fib_(x-1) + fib_(x-2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment