-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample4.java
More file actions
35 lines (29 loc) · 1.04 KB
/
Example4.java
File metadata and controls
35 lines (29 loc) · 1.04 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
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Example4 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
Future<String> future = fetchData(20);
//Bloqueo de la tarea
String data = future.get();
System.out.println(data);
synchronousTask2();
}
public static Future<String> fetchData(Integer id){
ExecutorService service = Executors.newSingleThreadExecutor();
return service.submit(() -> {
Thread.sleep(3000);
service.shutdown();
System.out.println("Tarea finalizada");
return "Datos obtenido del id: "+id;
});
}
public static void synchronousTask1() throws InterruptedException {
System.out.println("Obtuve datos");
Thread.sleep(10000);
}
public static void synchronousTask2(){
System.out.println("Calcular IVA");
}
}