Run multiple queries in parallel through threads

I have the following method:

public String getResult() { List<String> serversList = getServerListFromDB(); List<String> appList = getAppListFromDB(); List<String> userList = getUserFromDB(); return getResult(serversList, appList, userList); } 

Here I call three methods sequentially, which in turn gets into the database and extracts the results, then I process according to the results obtained from the database results. I know how to call these three methods simultaneously with Threads . But for this I would like to use Java 8 Parallel Stream . Can anyone advise me how to achieve the same through parallel threads?

EDIT I just want to call methods in parallel through Stream.

 private void getInformation() { method1(); method2(); method3(); method4(); method5(); } 
+6
source share
4 answers

You can use CompletableFuture as follows:

 public String getResult() { // Create Stream of tasks: Stream<Supplier<List<String>>> tasks = Stream.of( () -> getServerListFromDB(), () -> getAppListFromDB(), () -> getUserFromDB()); List<List<String>> lists = tasks // Supply all the tasks for execution and collect CompletableFutures .map(CompletableFuture::supplyAsync).collect(Collectors.toList()) // Join all the CompletableFutures to gather the results .stream() .map(CompletableFuture::join).collect(Collectors.toList()); // Use the results. They are guaranteed to be ordered in the same way as the tasks return getResult(lists.get(0), lists.get(1), lists.get(2)); } 
+8
source

foreach used for side-effects , you can call foreach on a parallel stream . eg:

 listOfTasks.parallelStream().foreach(list->{ submitToDb(list); }); 

However, parallelStream uses a common ForkJoinPool , which may not be suitable for IO-bound tasks.

Consider using CompletableFuture and set the appropriate ExecutorService . This gives a lot of flexibility ( continuation , configuration). For example:

 ExecutorService executorService = Executors.newCachedThreadPool(); List<CompletableFuture> allFutures = new ArrayList<>(); for(Query query:queries){ CompletableFuture<String> query = CompletableFuture.supplyAsync(() -> { // submit query to db return result; }, executorService); allFutures.add(query); } CompletableFuture<Void> all = CompletableFuture.allOf(allFutures.toArray(new CompletableFuture[allFutures.size()])); 
+2
source

As already mentioned, a standard parallel thread is probably not suitable for your use case. I performed each task asynchronously using the ExecutorService and β€œjoin” them when the getResult method was called:

 ExecutorService es = Executors.newFixedThreadPool(3); Future<List<String>> serversList = es.submit(() -> getServerListFromDB()); Future<List<String>> appList = es.submit(() -> getAppListFromDB()); Future<List<String>> userList = es.submit(() -> getUserFromDB()); return getResult(serversList.get(), appList.get(), userList.get()); 
+2
source

It's not entirely clear what you mean, but if you just want to run some process on these lists in parallel, you can do something like this:

  List<String> list1 = Arrays.asList("1", "234", "33"); List<String> list2 = Arrays.asList("a", "b", "cddd"); List<String> list3 = Arrays.asList("1331", "22", "33"); List<List<String>> listOfList = Arrays.asList(list1, list2, list3); listOfList.parallelStream().forEach(list -> System.out.println(list.stream().max((o1, o2) -> Integer.compare(o1.length(), o2.length())))); 

(it will print the longest items from each list).

+1
source

All Articles