我已經寫了下面的代碼:如何問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