Java Magazine, September/October 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 80 fix this Missing package and import statements If sample code does not include package or import statements and the question does not explicitly refer to these missing statements then assume that all sample code is in the same package or import statements exist to support them Therefore you should assume that the parent and child classes are in the same package Question 3 The correct answers are options D and E The ExecutorService interface is used to encapsulate thread pools Such a thread pool supports the execution of code presented to the pool using either the Runnable or Callable interface While the Runnable interface defines a method run that does not declare any checked exceptions the Callable interface defines a method call which throws Exception Because of this option C is wrong the call method can throw any exception that suits it Of course such an exception must be handled and its not possible for the pool infrastructure to make a semantic decision about how this should be done The submit method of the ExecutorService returns a Future object That Future object is really just a job handle and it provides several useful ways of interacting with the job after submission One such behavior is the get method which is used to obtain the result of the completed job If the job throws an exception from the call method the get method must report that exception back to its caller Also the get method will block until the job is completed if necessary and its a general convention that any blocking method can be interrupted If this happens the blocking call terminates abnormally and throws an InterruptedException to the caller The get method adheres to this convention and of course InterruptedException is a checked exception therefore code calling get must address this according to the normal rules of Javas exception mechanism From this you can deduce that option D is correct Notice that the call method in the Callable interface is declared as throws Exception so the implementation could properly throw an InterruptedException Because the get method throws a checked exception for its own reasons it would be confusing if the same exception could arise from the get method for two diferent reasons Because of this along with a gen
You must have JavaScript enabled to view digital editions.