Given the code fragment:

class CallerThread implements Callable<String> {

    String str;

    public CallerThread(String s) {
        this.str = s;
    }

    public String call() throws Exception {
        return str.concat(" Call");
    }
}

and

public static void main(String[] args) throws InterruptedException, ExecutionException {
    ExecutorService es = Executors.newFixedThreadPool(4); //line n1
    Future f1 = es.submit(new CallerThread("Call"));
    String str = f1.get().toString();
    System.out.println(str);
}

Which statement is true?

A. The program prints Call Call and terminates.
B. The program prints Call Call and does not terminate.
C. A compilation error occurs at line n1.
D. An ExecutionException is thrown at run time.

题解

程序第 32 行,实体化出 CallerThread 的对象并透过 ExecutorService 来运行。

程序第 33 行,会等待 CallerThread 对象的 call 方法运行完毕后,将结果回传,保存至 str 变量中。回传的对象为「Call Call」字符串。

程序第 34 行,输出「Call Call」。

由于没有调用 ExecutorService 对象的 shutdown 相关方法,因此程序不会停止运行。