沉默可能产生误解,我需要说话,说话将我推向歧途,我必须沉默。赫塔·米勒——《国王鞠躬,国王杀人》
好问题,尝试一下
import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Stream; class Scratch { public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException { CompletableFuture.runAsync(() -> Stream.generate(Object::new).forEach(System.out::println)).get(2, TimeUnit.SECONDS); Thread.currentThread().join(); } }
发现两秒后断开了
那我们try
下异常
import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Stream; class Scratch { public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException { try { CompletableFuture.runAsync(() -> Stream.generate(Object::new).forEach(System.out::println)).get(2, TimeUnit.SECONDS); } catch (TimeoutException e) { System.err.println("Timeout"); } Thread.currentThread().join(); } }
并没有断开,仍然在执行
因此,CompletableFuture
的异步任务如果超时异常try
掉了,是不会停止执行的