Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.stream.IntStream;
- public class EvenOddWithES {
- public static void main(String[] args) {
- ExecutorService executorService = Executors.newFixedThreadPool(2);
- IntStream.range(1, 10).forEach(num -> {
- CompletableFuture<Integer> thenApplyAsync = CompletableFuture.completedFuture(num).thenApplyAsync(x -> {
- if (x % 2 != 0) {
- System.out.println(x + " " + Thread.currentThread().getName());
- }
- return num;
- }, executorService);
- thenApplyAsync.join();
- CompletableFuture<Integer> thenApplyAsync2 = CompletableFuture.completedFuture(num).thenApplyAsync(x -> {
- if (x % 2 == 0) {
- System.out.println(x + " " + Thread.currentThread().getName());
- }
- return num;
- }, executorService);
- thenApplyAsync2.join();
- });
- executorService.shutdown();
- }
- }
Advertisement
Advertisement