223. Combining StructuredTaskScope and streams
If you prefer functional programming, then you’ll be happy to see that streams can be used with StructuredTaskScope
as well. For instance, here we rewrite the application from Problem 221, using a stream pipeline to fork our tasks:
public static TestingTeam buildTestingTeam()
throws InterruptedException, ExecutionException {
try (ShutdownOnSuccess scope
= new StructuredTaskScope.ShutdownOnSuccess<String>()) {
Stream.of(1, 2, 3)
.<Callable<String>>map(id -> () -> fetchTester(id))
.forEach(scope::fork);
scope.join();
String result = (String) scope.result();
logger.info(result);
return new TestingTeam(result);
}
}
Moreover, we can use stream pipelines to collect results and exceptions, as follows:
public static TestingTeam buildTestingTeam()
throws InterruptedException, ExecutionException {
try (ShutdownOnSuccess scope
= new StructuredTaskScope...