0
我正在使用ThreadPool執行程序在do while循環中作爲deamon進程。我在創建新的固定線程池: executor = Executors.newFixedThreadPool(5);執行程序ThreadPool,垃圾回收
任務完成後,我在下一次迭代開始前調用shutdown,併爲new 5 worker線程創建newFixedPool。
現在打印Log4J日誌。我正在使用[%t]來顯示執行哪個線程池。 我看到每個迭代新線程池獲得創建一個遞增的數字。
請讓我知道在開始下一次迭代之前,我需要調用executor = null嗎?
遞增的線程池指示線程池獲取累積並可能導致內存泄漏,因爲它是一個deamon進程或在後端線程池將被垃圾收集,因爲所有線程都已完成並且我們已經執行了關閉。
2014-06-26 16:26:11,548 [47 ] [INFO ][pool-1-thread-1] - Started processing new payments for serverID: 5
2014-06-26 16:26:11,548 [47 ] [INFO ][pool-1-thread-2] - Started processing new payments for serverID: 6
2014-06-26 16:26:11,548 [47 ] [INFO ][pool-1-thread-3] - Started processing new payments for serverID: 7
2014-06-26 16:26:11,548 [47 ] [INFO ][pool-1-thread-4] - Started processing new payments for serverID: 8
2014-06-26 16:26:11,548 [47 ] [INFO ][pool-1-thread-5] - Started processing new payments for serverID: 9
2014-06-26 16:26:24,129 [12628 ] [INFO ][pool-1-thread-2] - Completed processing new payments for serverID: 6
2014-06-26 16:26:24,129 [12628 ] [INFO ][pool-1-thread-5] - Completed processing new payments for serverID: 9
2014-06-26 16:26:24,129 [12628 ] [INFO ][pool-1-thread-2] - Time taken to Process new payments for serverId 6 : 12 Sec
2014-06-26 16:26:24,129 [12628 ] [INFO ][pool-1-thread-5] - Time taken to Process new payments for serverId 9 : 12 Sec
2014-06-26 16:26:24,129 [12628 ] [INFO ][pool-1-thread-3] - Completed processing new payments for serverID: 7
2014-06-26 16:26:24,129 [12628 ] [INFO ][pool-1-thread-1] - Completed processing new payments for serverID: 5
2014-06-26 16:26:24,129 [12628 ] [INFO ][pool-1-thread-3] - Time taken to Process new payments for serverId 7 : 12 Sec
2014-06-26 16:26:24,129 [12628 ] [INFO ][pool-1-thread-1] - Time taken to Process new payments for serverId 5 : 12 Sec
2014-06-26 16:26:24,129 [12628 ] [INFO ][pool-1-thread-4] - Completed processing new payments for serverID: 8
2014-06-26 16:26:24,129 [12628 ] [INFO ][pool-1-thread-4] - Time taken to Process new payments for serverId 8 : 12 Sec
2014-06-26 16:26:24,129 [12628 ] [INFO ][main] - ShutDown complete
2014-06-26 16:26:24,129 [12628 ] [INFO ][main] - ProcessDeamon iteration finished in 0 Sec
2014-06-26 16:26:24,129 [12628 ] [INFO ][main] - ProcessDeamon iteration started
2014-06-26 16:26:24,191 [12690 ] [INFO ][pool-2-thread-1] - Started processing new payments for serverID: 5
2014-06-26 16:26:24,191 [12690 ] [INFO ][pool-2-thread-2] - Started processing new payments for serverID: 6
2014-06-26 16:26:24,191 [12690 ] [INFO ][pool-2-thread-3] - Started processing new payments for serverID: 7
2014-06-26 16:26:24,191 [12690 ] [INFO ][pool-2-thread-4] - Started processing new payments for serverID: 8
2014-06-26 16:26:24,191 [12690 ] [INFO ][pool-2-thread-5] - Started processing new payments for serverID: 9
2014-06-26 16:26:34,443 [22942 ] [INFO ][pool-2-thread-2] - Completed processing new payments for serverID: 6
2014-06-26 16:26:34,443 [22942 ] [INFO ][pool-2-thread-2] - Time taken to Process new payments for serverId 6 : 10 Sec
2014-06-26 16:26:34,490 [22989 ] [INFO ][pool-2-thread-4] - Completed processing new payments for serverID: 8
2014-06-26 16:26:34,490 [22989 ] [INFO ][pool-2-thread-3] - Completed processing new payments for serverID: 7
2014-06-26 16:26:34,490 [22989 ] [INFO ][pool-2-thread-5] - Completed processing new payments for serverID: 9
2014-06-26 16:26:34,490 [22989 ] [INFO ][pool-2-thread-4] - Time taken to Process new payments for serverId 8 : 10 Sec
2014-06-26 16:26:34,490 [22989 ] [INFO ][pool-2-thread-3] - Time taken to Process new payments for serverId 7 : 10 Sec
2014-06-26 16:26:34,490 [22989 ] [INFO ][pool-2-thread-5] - Time taken to Process new payments for serverId 9 : 10 Sec
2014-06-26 16:26:34,521 [23020 ] [INFO ][pool-2-thread-1] - Completed processing new payments for serverID: 5
2014-06-26 16:26:34,521 [23020 ] [INFO ][pool-2-thread-1] - Time taken to Process new payments for serverId 5 : 10 Sec
2014-06-26 16:26:34,521 [23020 ] [INFO ][main] - ShutDown complete
2014-06-26 16:26:34,521 [23020 ] [INFO ][main] - ProcessDeamon iteration finished in 0 Sec
2014-06-26 16:26:34,521 [23020 ] [INFO ][main] - ProcessDeamon iteration started
我無法重用......因爲我希望所有迭代都一致。這就是原因,我打電話給關機...然後等待無限循環來檢查執行程序是否終止。執行關機後,不能重複使用同一個池...從文檔中...執行程序不會接受新的任務.. – Apt
請閱讀並理解「ThreadpoolExecutor」的API。不要等待無限循環,而是使用「awaitTermination」。如果你想在同一個'Threadpool'中執行多批任務,不要關閉那個池,而是等待'getActiveCount == 0'成爲true – Jonathan