-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample5.java
More file actions
36 lines (29 loc) · 1.03 KB
/
Example5.java
File metadata and controls
36 lines (29 loc) · 1.03 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
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class Example5 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
CompletableFuture<String> completableFuture = fetchData(20);
//No bloqueo
completableFuture.thenAccept(value -> {
System.out.println(value);
});
synchronousTask2();
synchronousTask1();
}
public static CompletableFuture<String> fetchData(Integer id){
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {}
return "Datos obtenido del id: "+id;
});
}
public static void synchronousTask1() throws InterruptedException {
System.out.println("Obtuve datos");
Thread.sleep(10000);
System.out.println("Finalice obtener datos");
}
public static void synchronousTask2(){
System.out.println("Calcular IVA");
}
}