我已经写了下面的代码:如何问CompletableFuture使用非守护线程?
System.out.println("Main thread:" + Thread.currentThread().getId());
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try {
System.out.println("Before sleep thread:" + Thread.currentThread().getId(), + " isDaemon:" + Thread.currentThread().isDaemon());
Thread.sleep(100);
System.out.println("After sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
future.whenComplete((r, e) -> System.out.println("whenCompleted thread:" + Thread.currentThread().getId()));
而这一次打印:
Main thread:1
Before sleep thread:11 isDaemon:true
和完成。
我该如何改变这种行为?
P.S.我没有看到任何东西runAsync
Java文档相关
@GhostCat,我只是在写测试,这种行为令我感到惊讶 – gstackoverflow
@GhostCat感谢您的查询) – gstackoverflow