I would like to refactor the following code
Set<String> allStrings ...
Set<MyTask> myTasks = ...
myTasks.add(new MyTask());
myTasks.add(new MyTask());
...
List<Future<Set<String>>> futures = executorService.invokeAll(myTasks);
executorService.shutdown();
for (Future<Set<String>> future : futures) {
Set<String> strings = future.get();
allStrings.addAll(strings);
}
// do stuff with allStrings
....
such that the allStrings
is passed to MyTask
where the variable is populated inside the class and the Future returns Void
:
Set<String> allStrings ...
Set<MyTask> myTasks = ...
myTasks.add(new MyTask(allStrings));
myTasks.add(new MyTask(allStrings));
...
executorService.invokeAll(myTasks);
executorService.shutdown();
// do stuff with allStrings
....
Is it okay to do so? Is there any concurrency thing that I have to worry about because the results seem to be different (in my actual code - not this code)?
Also, can I assume that all tasks are completed such that I do not have to do a dummy loop before doing stuff with allStrings
:
for (Future<Set<String>> future : futures) {
future.get();
}
// do stuff with `allStrings`