ExecutorService和Service是接口,因此只有抽象方法,这意味着它们的方法没有实现.我们再怎么称呼,例如future.get(),es.submit()和es.shutdown()方法的接口类型的参考?例如,我们为什么要这样做呢?
Future f = ...
f.get();
Run Code Online (Sandbox Code Playgroud)
这是一个更具体的例子:
import java.util.concurrent.*;
class Factorial implements Callable<Long> {
long n;
public Factorial(long n) {
this.n = n;
}
public Long call() throws Exception {
if (n <= 0) {
throw new Exception("for finding factorial, N should be > 0");
}
long fact = 1;
for(long longVal = 1; longVal <= n; longVal++) {
fact *= longVal;
}
return fact;
}
}
class CallableTest {
public static void …Run Code Online (Sandbox Code Playgroud)