forked from forax/loom-fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_13_timeout.java
30 lines (28 loc) · 1019 Bytes
/
_13_timeout.java
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
package fr.umlv.loom.example;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;
import java.util.concurrent.TimeoutException;
// $JAVA_HOME/bin/java --enable-preview -cp target/classes fr.umlv.loom.example._13_timeout
public interface _13_timeout {
static void main(String[] args) throws InterruptedException, ExecutionException {
try (var scope = new StructuredTaskScope<>()) {
var task1 = scope.fork(() -> {
Thread.sleep(1_000); // throws InterruptedException
return 1;
});
var task2 = scope.fork(() -> {
Thread.sleep(5_000); // throws InterruptedException
return 2;
});
try {
scope.joinUntil(Instant.now().plus(Duration.ofMillis(100)));
} catch (TimeoutException e) {
//scope.shutdown();
}
System.out.println(task1.state()); // UNAVAILABLE
System.out.println(task2.state()); // UNAVAILABLE
}
}
}