ShutdownOnSuccess
只要有一个任务成功将整个StructuredTaskScope
标记为成功。取消其他任务。
ShutdownOnFailure
用于需要全部成功的场景,有一个失败, 那么StructuredTaskScope
标记为失败, 取消未完成的任务(Interrupt or Cancel)。
//获取表示时间的Instant对象
Instant instant = LocalDateTime.now().plus(10, ChronoUnit.SECONDS)
.atZone(ZoneId.systemDefault()).toInstant();
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
Subtask<Shelter> shelter = scope.fork(this::getShelter);
Subtask<List<Dog>> dogs = scope.fork(this::getDogs);
//带超时的join
scope.joinUntil(instant);
//打印异常
scope.exception().ifPresent(e-> log.error(e.getMessage(), e));
Response response = new Response(shelter.get(), dogs.get());
log.info("{}", response);
}
Posted in: java基础
Comments are closed.