Exam 1z0-829 All QuestionsBrowse all questions from this exam
Question 30

Given the code fragment:

Which code fragment invokes all callable objects in the workers set?

    Correct Answer: D

    The correct code fragment to invoke all callable objects in the workers set is to use the invokeAll method provided by the ExecutorService. This method returns a list of Future objects representing the pending results of the tasks. Option D correctly uses the invokeAll method, and then iterates over the resulting futures to print out the results. Other options referenced non-existent variables 'cThreads' or used the invokeAny method incorrectly.

Discussion
james2033Option: D

// D. List<Future<String>> futures = executorService.invokeAll(workers); for (Future<String> future : futures) { System.out.println(future.get()); }

supersquaxOption: D

D is correct. A and B reference "cThreads" which isn't declared anywhere. C uses invokeAny(), which doesnt return a List. It returns the result of the first successfully completed task.