Java - Structured Concurrency in JDK 21
As a part of loom project, JDK21 will support the structured concurrency to handle multiple concurrent tasks in a structured way.
1
2
3
4
5
6
7
8
9
10
11
Response handle() throws ExecutionException, InterruptedException {
try (var scope = StructuredTaskScope.ShutdownOnFailure()) {
Future<String> user = scope.fork(() -> findUser());
Future<String> fetchOrder = scope.fork(() -> fetchOrder());
scope.join();
scope.throwIfFailed()
return Response(user.resultNow(), fetchOrder.resultNow());
}
}