我的应用程序中有类似的代码。
@Service
public class MyService {
public List<String> getvalues(Long userId) {
List<String> values = Collections.synchronizedList(new ArrayList());
CompletableFuture<Void> result01 = CompletableFuture
.supplyAsync(() -> externalService.getvalues())
.thenaccept(s -> values.add(s));
CompletableFuture<Void> result02 = CompletableFuture
.supplyAsync(() -> externalService.getOtherValues())
.thenaccept(s -> values.add(s));
try {
CompletableFuture.allOf(result01,result02).get();
} catch (InterruptedException | ExecutionException e) {
log.error("Error while get user products",e);
Thread.currentThread().interrupt();
}
return values;
}
}
我正在这样做:
try {
CompletableFuture.allOf(result01,result02).get();
} catch (InterruptedException | ExecutionException e) {
new IllegalStateException("Fail while get products");
}
当我这样做时,声纳打开了这个问题: “ InterruptedException”不容忽视
我了解了很多有关interruptedException的信息。
我的疑问是: 如果将currentThread标记为中断,则可能在此方法之后运行的其他方法(在同一Thread中)可能再次引发InterruptedException,并且线程流将被中断。
如何处理而又不抛出InterruptedException?
我不想重新引发InterruptedException。
我正在使用Java 8和Spring Boot 2。