Advertisement
javatechie

Even odd with Executor service

Aug 14th, 2023 (edited)
1,303
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | Source Code | 0 0
  1. import java.util.concurrent.CompletableFuture;
  2. import java.util.concurrent.ExecutorService;
  3. import java.util.concurrent.Executors;
  4. import java.util.stream.IntStream;
  5.  
  6. public class EvenOddWithES {
  7.  
  8.     public static void main(String[] args) {
  9.         ExecutorService executorService = Executors.newFixedThreadPool(2);
  10.  
  11.         IntStream.range(1, 10).forEach(num -> {
  12.             CompletableFuture<Integer> thenApplyAsync = CompletableFuture.completedFuture(num).thenApplyAsync(x -> {
  13.                 if (x % 2 != 0) {
  14.                     System.out.println(x + " " + Thread.currentThread().getName());
  15.                 }
  16.                 return num;
  17.             }, executorService);
  18.             thenApplyAsync.join();
  19.  
  20.             CompletableFuture<Integer> thenApplyAsync2 = CompletableFuture.completedFuture(num).thenApplyAsync(x -> {
  21.                 if (x % 2 == 0) {
  22.                     System.out.println(x + " " + Thread.currentThread().getName());
  23.                 }
  24.                 return num;
  25.             }, executorService);
  26.             thenApplyAsync2.join();
  27.         });
  28.  
  29.         executorService.shutdown();
  30.     }
  31. }
  32.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement