-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompletableFuture
37 lines (26 loc) · 992 Bytes
/
CompletableFuture
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
37
CompletableFuture In Java8
So for above example if we want to execute in multithreading env using ThreadPool we have to implement the code like this.
try{
Future<Order> future = service.submit(getOrderTask());
Order order = future.get();
Future<Order> future1 = service.submit(enrichTask(order));
Order order = future1.get();
Future<Order> future2 = service.submit(performPaymentTask(order));
Order order = future2.get();
Future<Order> future3 = service.submit(dispatchTask(order));
Order order = future3.get();
Future<Order> future4 = service.submit(sendEmailTask(order));
Order order = future4.get();
}
catch(InterruptedException | ExecutionException e)
{
e.printStackTrace();
}
To Overcome this problem ComopletableFuture is introduced.
CompltableFutrue.supplyAsync(() ->getOrder())
.thenApply(order->enrich(order))
.thenApply(order->performPayment(order))
.thenApply(order->dispatch(order))
.thenapply(ordre->sendEmail(order));
}
}