-
Notifications
You must be signed in to change notification settings - Fork 229
/
EvenAndOddPrinterByES.java
36 lines (30 loc) · 1.46 KB
/
EvenAndOddPrinterByES.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.javatechie;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
public class EvenAndOddPrinterByES {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
IntStream.rangeClosed(1, 10)
.forEach(num -> {
CompletableFuture<Integer> oddCompletableFuture = CompletableFuture.completedFuture(num)
.thenApplyAsync(x -> {
if (x % 2 != 0) {
System.out.println("Thread Name " + Thread.currentThread().getName() + " : " + x);
}
return num;
}, executorService);
oddCompletableFuture.join();
CompletableFuture<Integer> evenCompletableFuture = CompletableFuture.completedFuture(num)
.thenApplyAsync(x -> {
if (x % 2 == 0) {
System.out.println("Thread Name " + Thread.currentThread().getName() + " : " + x);
}
return num;
}, executorService);
evenCompletableFuture.join();
});
executorService.shutdown();
}
}