Runnable:no return
Callable:return
Future interface#get methodでreturnを取得する
public class Outer {
public static void main(String[] args) throws InterruptedException {
ExecutorService e = Executors.newSingleThreadExecutor();
Runnable r = () -> {
System.out.println("runnable");
};
Callable<Integer> c = () -> {
return 1;
};
Future f = e.submit(r);
try {
System.out.println(f.get());
} catch (ExecutionException ex) {
Logger.getLogger(Outer.class.getName()).log(Level.SEVERE, null, ex);
}
Future<Integer> f2 = e.submit(r,0);
try {
System.out.println(f2.get());
} catch (ExecutionException ex) {
Logger.getLogger(Outer.class.getName()).log(Level.SEVERE, null, ex);
}
f2 = e.submit(c);
try {
System.out.println(f2.get());
} catch (ExecutionException ex) {
Logger.getLogger(Outer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
runnable
null
runnable
0
1