Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. public class AsyncStreamExecutorProcessing {
  2. private final ServiceInvoker serviceInvoker;
  3. private final ExecutorService executorService = Executors.newFixedThreadPool(100);
  4.  
  5. public AsyncStreamExecutorProcessing() {
  6. this.serviceInvoker = new ServiceInvoker();
  7. }
  8.  
  9. public static void main(String[] args) {
  10. new AsyncStreamExecutorProcessing().start();
  11. }
  12.  
  13. private void start() {
  14. List<String> ids = Arrays.asList(
  15. "C01", "C02", "C03", "C04", "C05", "C06", "C07", "C08", "C09", "C10",
  16. "C11", "C12", "C13", "C14", "C15", "C16", "C17", "C18", "C19", "C20");
  17.  
  18. long startTime = System.nanoTime();
  19. List<CompletableFuture<Client>> futureRequests = ids.stream()
  20. .map(id -> CompletableFuture.supplyAsync(() -> serviceInvoker.invoke(id), executorService))
  21. .collect(toList());
  22.  
  23. double totalPurchases = futureRequests.stream()
  24. .map(CompletableFuture::join)
  25. .collect(summingDouble(Client::getPurchases));
  26.  
  27. long endTime = (System.nanoTime() - startTime) / 1_000_000;
  28. System.out.println("Async with executor | Total time: " + endTime + " ms");
  29. System.out.println("Total purchases: " + totalPurchases);
  30.  
  31. executorService.shutdown();
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement