There are two ways to run it in parallel: threads and artists.
Using threads
You can use parallel threads and leave the rest in jvm. In this case, you do not have too much control over what happens when. On the other hand, your code will be easy to read and maintain:
sellerDataList.stream().forEach(sellerNames -> { Stream<String> stream = StreamSupport.stream(sellerNames.spliterator(), true);
Using ExecutorService
Suppose you want 5 threads and you want to wait for the task to complete. Then you can use a fixed thread pool with 5 threads and use Future -s so that you can wait until they are executed.
final ExecutorService executor = Executors.newFixedThreadPool(5); // it just an arbitrary number final List<Future<?>> futures = new ArrayList<>(); for (SellerNames sellerNames : sellerDataList) { for (final String sellerName : sellerNames) { Future<?> future = executor.submit(() -> { getSellerAddress(sellerName); }); futures.add(future); } } try { for (Future<?> future : futures) { future.get(); // do anything you need, eg isDone(), ... } } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); }
source share