请参阅ExecutorService的文档,其中包含两个提交方法和一个参数:
你的lambda给出一个输出,返回一些东西:
executorService.submit(() -> {
System.out.println("Starting");
int n = new Random().nextInt(4000);
// try-catch-finally omitted
return n; //
});
所以lambda必须是Callable< Integer>这是一个快捷方式:
executorService.submit(new Callable() {
@Override
public Integer call() throws Exception {
System.out.println("Starting");
int n = new Random().nextInt(4000);
// try-catch-finally omitted
return n;
}}
);
要进行比较,请尝试使用Runnable,并且您看到它的方法的返回类型为void.
executorService.submit(new Runnable() {
@Override
public void run() {
// ...
}}
);