Skip to content

Instantly share code, notes, and snippets.

@thlemercier
Created May 8, 2019 23:02
Show Gist options
  • Save thlemercier/d0824ff76e18d7b0d7a7660a8c86980d to your computer and use it in GitHub Desktop.
Save thlemercier/d0824ff76e18d7b0d7a7660a8c86980d to your computer and use it in GitHub Desktop.
// The executor in charge of executing the different crawls in its thread pool
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
// Start all the crawl tasks in a separate Thread and get the results of each tasks in a Future
List<Future<List<MyDto>>> tasksResults = myList.stream()
.map(item -> {
// Create an instance of a Class that implements Callable<List<MyDto>> {
return executor.submit(instanceOfCallable);
}).collect(Collectors.toList());
// For each Futur from Threads
tasksResults.stream().forEach(futureResult -> {
try {
List<MyDto> myDtos = futureResult.get();
if ( myDtos.size() > 0 ) {
// Do Stuff here
}
}
catch ( InterruptedException | ExecutionException e ) {
e.printStackTrace();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment