What is the output of the following application?.
package mypkg; //w w w .j av a 2s .com import java.util.concurrent.*; public class Main { public static void completePaperwork() { System.out.print("[Filing]"); } public static double getPi() { return 3.14159; } public static void main(String[] args) throws Exception { ExecutorService x = Executors.newSingleThreadExecutor(); Future<?> f1 = x.submit(() -> completePaperwork()); Future<Object> f2 = x.submit(() -> getPi()); System.out.print(f1.get()+" "+f2.get()); x.shutdown(); } }
A.
The code compiles without issue, so Options B and C are incorrect.
The f1 declaration uses the version of submit()
in ExecutorService, which takes a Runnable and returns a Future<?>.
The call f1.get()
waits until the task is finished and always returns null, since Runnable expressions have a void return type.
The f2 declaration uses an overloaded version of submit()
, which takes a Callable expression and returns a generic Future object.
Since the double value can be autoboxed to a Double object, the line compiles without issue with f2.get()
returning 3.14159.
Option A is the correct answer.
Option D is incorrect because no exception is expected to be thrown at runtime.