Promise
@Slf4j
public class Promise {
/**
* Promise Pattern【承诺】:承诺表示当前还未完成,但是会在将来完成的操作,用于实现异步计算。
*/
@Test
public void all() throws InterruptedException, ExecutionException {
final CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(2);
} catch (final InterruptedException e1) {
throw new RuntimeException(e1);
}
return "hello";
}).exceptionally(e -> {
log.error("error happened", e);
return "error";
});
final String result = future.get();
assertEquals("hello", result);
}
}