-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompletableFutureDemo.java
More file actions
127 lines (115 loc) · 5.65 KB
/
CompletableFutureDemo.java
File metadata and controls
127 lines (115 loc) · 5.65 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package java;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
public class CompletableFutureDemo {
public static double toFahrenheit(int celsius){
return (celsius * 1.8 + 32);
}
public static CompletableFuture <String> getUserEmailAsync(){
return CompletableFuture.supplyAsync(() -> "email");
}
public static CompletableFuture <String> getUserPlaylistAsync(String email){
return CompletableFuture.supplyAsync(() -> "playlist");
}
public static void main(String[] args) {
Runnable task = () -> System.out.println("rahul");
var result = CompletableFuture.runAsync(task); // runAsync is overloaded as (Runnable, Executor),
//by default it uses ForkJoinPool.commonPool()
System.out.println(result);
Supplier<Integer> task2 = () -> 1;
var result2 = CompletableFuture.supplyAsync(task2);
try {
System.out.println(result2.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
/*-------------------Asynchronous API----------------------------- */
var service = new MailService();
service.sendAsync();
System.out.println("Hello World!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("--------------------------------------------");
/*-------------------------Running code on completion------------------------------ */
var future = CompletableFuture.supplyAsync(() -> 1);
future.thenRun(() -> //runs on main thread.
{
System.out.println(Thread.currentThread().getName());
System.out.println("Running..");
});
System.out.println("--------------------------------------------");
future.thenRunAsync(() -> //runs on commonpool.
{
System.out.println(Thread.currentThread().getName());
System.out.println("Running..");
});
future.thenAcceptAsync(result3 ->
{
System.out.println(Thread.currentThread().getName());
System.out.println(result3);
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("-----------------------------------");
/*---------------------------Transforming completable future--------------------- */
var future3 = CompletableFuture.supplyAsync(() -> 19);
future3.thenApply(CompletableFutureDemo :: toFahrenheit)
.thenAccept(System.out :: println);
// general implementation.
// try {
// var future4 = future3.thenApply(t -> t*1.8 + 32).get();
// System.out.println("Temperature in Fahrenheit : "+ future4);
// } catch (InterruptedException | ExecutionException e) {
// e.printStackTrace();
// }
System.out.println("-----------------------------");
/*----------------------composing completable future----------------------- */
getUserEmailAsync()
.thenCompose(CompletableFutureDemo :: getUserPlaylistAsync)
.thenAccept(System.out::println);
// General composing of completablefuture.
// CompletableFuture.supplyAsync(() -> "email")
// .thenCompose(email -> CompletableFuture.supplyAsync(() -> "playlist"))
// .thenAccept(System.out :: println);
System.out.println("-----------------------------");
var first = CompletableFuture.supplyAsync(() -> "36USD")
.thenApply(str -> {
var price = str.replace("USD", "");
return Integer.parseInt(price);
});
var second = CompletableFuture.supplyAsync(() -> 1.6);
first.thenCombine(second, (price, exchangeRate)-> price * exchangeRate)
.thenAccept(System.out::println);
System.out.println("-----------------------------");
/*-----------------------------------waiting for many tasks----------------------- */
var x = CompletableFuture.supplyAsync(() -> 1);
var y = CompletableFuture.supplyAsync(() -> 2);
var z = CompletableFuture.supplyAsync(() -> 3);
var all = CompletableFuture.allOf(x, y, z);
all.thenRun(()-> {
try {
var first1 = x.get();
System.out.println(first1);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
System.out.println("All tasks are completed.");
System.out.println("------------------------------");
/*------------------------------Any of the provides Tasks---------------- */
var firstTask = CompletableFuture.supplyAsync(() -> {
LongTask.simulate();
return 28;
});
var secondTask = CompletableFuture.supplyAsync(() -> 18);
CompletableFuture.anyOf(firstTask, secondTask)
.thenAccept(System.out::println);
}
}