让我们假设以下主要方法:
public class Async {
public static void main(String[] args) throws Exception {
CompletableFuture.supplyAsync(Async::sendMsg);
System.out.println(Thread.currentThread().getName());
}
public static String sendMsg() {
try {
TimeUnit.SECONDS.sleep(5);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
我只想进行异步调用而不阻塞主线程。但控制台只输出以下字符串:
main
的输出sendMsg
- 方法似乎没有被调用。但为什么?我错过了什么吗?
这是因为CompletableFuture.supplyAsync(供应商)使用普通ForkJoinPool一旦程序(主线程)终止,任务就会自动终止。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)