Which statements about the following application are true?
package mypkg; /*from w w w .j a va 2s.c o m*/ import java.util.concurrent.*; public class Plan { private ExecutorService service = Executors.newCachedThreadPool(); public void planEvents() { service.scheduleWithFixedDelay( () -> System.out.print("A"), 1, TimeUnit.HOURS); service.scheduleAtFixedRate( () -> System.out.print("B"), 1, 1000, TimeUnit.SECONDS); service.execute(() -> System.out.print("C")); } }
scheduleWithFixedDelay()
method call compiles.scheduleAtFixedRate()
method call compiles.execute()
method call compiles.C.
Both schedule method calls do not compile because these methods are only available in the ScheduledExecutorService interface, not the ExecutorService interface.
Even if the correct reference type for service was used, along with a compatible Executors factory method, the scheduleWithFixedDelay()
call would still not compile because it only contains a single numeric value.
This method requires two long values, one for the initial delay and one for the period.
The execute()
method call compiles without issue because this method is available in the ExecutorService interface.
For these reasons, only the third statement is true, making Option C the correct answer.