Given the code fragment:
Which code fragment invokes all callable objects in the workers set?
Given the code fragment:
Which code fragment invokes all callable objects in the workers set?
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.
// D. List<Future<String>> futures = executorService.invokeAll(workers); for (Future<String> future : futures) { System.out.println(future.get()); }
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.