forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetBlocking.java
More file actions
33 lines (22 loc) · 908 Bytes
/
GetBlocking.java
File metadata and controls
33 lines (22 loc) · 908 Bytes
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
package com.zetcode;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class GetBlocking {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.printf("Task run in: %s %n", Thread.currentThread().getName());
// Simulate a long-running job
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "success";
});
System.out.printf("This is %s thread %n", Thread.currentThread().getName());
// blocking main thread
var result = future.get();
System.out.println(result);
}
}