forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunBlocking.java
More file actions
29 lines (20 loc) · 837 Bytes
/
RunBlocking.java
File metadata and controls
29 lines (20 loc) · 837 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
package com.zetcode;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class RunBlocking {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
// Simulate a long-running job
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
System.out.printf("Task run in: %s %n", Thread.currentThread().getName());
});
future.whenComplete((aVoid, throwable) -> System.out.println("completed"));
future.get();
System.out.println("Program finished");
}
}