[英]Are worker threads in ForkJoinPool are Daemon threads?
我正在閱讀Java - The Complete Reference一書中有關 Fork/Join Framework 的信息。 它說ForkJoinPool
使用守護線程:
ForkJoinPool 使用守護線程。 當所有用戶線程都已終止時,守護程序線程會自動終止。 因此,無需顯式關閉 ForkJoinPool。 但是,除了公共池之外,可以通過調用 shutdown() 來實現。 shutdown() 方法對公共池沒有影響。
ForkJoinWorkerThread
都是守護線程?ForkJoinPool
用於重要任務嗎?shutdown()
是否等待工作線程完成?1.
jshell> ForkJoinPool.commonPool().execute(() -> System.out.println(Thread.currentThread().getClass() + " isDaemon? " + Thread.currentThread().isDaemon()))
class java.util.concurrent.ForkJoinWorkerThread isDaemon? true
jshell>
答:是的,它們是守護線程。
2.
jshell> new Thread(() -> System.out.println("isDaemon? " + Thread.currentThread().isDaemon() + " priority:" + Thread.currentThread().getPriority())).start()
isDaemon? false priority:5
jshell> ForkJoinPool.commonPool().execute(() -> System.out.println("isDaemon? " + Thread.currentThread().isDaemon() + " priority:" + Thread.currentThread().getPriority()))
isDaemon? true priority:5
A : ForkJoinPool 默認創建與其他線程具有相同優先級的線程。
3.
來自 ForkJoinPool#commonPool 的 javadoc
返回公共池實例。 這個池是靜態構建的; 它的運行 state 不受 shutdown() 或 shutdownNow() 嘗試的影響。 然而,這個池和任何正在進行的處理都會在程序 System.exit(int) 時自動終止。 任何依賴異步任務處理在程序終止之前完成的程序都應該在退出之前調用 commonPool().awaitQuiescence。
A : ForkJoinPool 忽略關閉,但應用程序可以調用awaitQuiescence以確保所有任務都完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.