【发布时间】:2019-03-23 01:15:03
【问题描述】:
我正在测试CompletableFuture 的工作原理。我对如何并行执行任务感兴趣:
try {
CompletableFuture one = CompletableFuture.runAsync(() -> {
throw new RuntimeException("error");
});
CompletableFuture two = CompletableFuture.runAsync(() -> System.out.println("2"));
CompletableFuture three = CompletableFuture.runAsync(() -> System.out.println("3"));
CompletableFuture all = CompletableFuture.allOf(one, two, three);
all.get();
} catch (InterruptedException e) {
System.out.println(e);
} catch (ExecutionException e) {
System.out.println(e);
}
在这种情况下,它们将被全部执行。
1。当其中一个线程出现异常时,是否可以中断所有正在运行的线程?
2。当这段代码在一个可以从不同线程调用的类方法中时,它是线程安全的吗?
【问题讨论】:
标签: java multithreading java-8 completable-future