【问题标题】:Executor ThreadPool, garbage collectionExecutor ThreadPool,垃圾回收
【发布时间】:2014-06-26 09:40:50
【问题描述】:

我在 do while 循环中使用 ThreadPool 执行器作为守护进程。 在每次迭代开始时,我都会创建 newFixedThreadPool: executor = Executors.newFixedThreadPool(5);

任务完成后,我在下一次迭代开始之前调用 shutdown 并为新的 5 个工作线程创建 newFixedPool。

现在正在打印 Log4J 日志。我正在使用 [%t] 来显示执行哪个线程池。 我看到每次迭代都会使用递增的数字创建新的线程池。

请让我知道在开始下一次迭代之前我需要调用 executor=null 吗?

增加的线程池是否表明,线程池累积并可能导致内存泄漏,因为它是一个守护进程或在后端线程池将被垃圾收集,因为所有线程都已完成并且我们已经执行了关闭。

 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

【问题讨论】:

    标签: garbage-collection threadpoolexecutor


    【解决方案1】:

    不要关闭您的线程池,而是重新使用它。这就是拥有一个线程池的全部意义:您将任务提交到这个池中,而不必关心线程管理。只需等待您的线程终止并将下一批任务提交到同一个池中即可。

    【讨论】:

    • 我不能重用...因为我希望所有迭代都保持一致。这就是原因,我正在调用shutdown ...然后在无限循环中等待以检查执行程序是否终止。执行关机后..无法重用与文档中相同的池..执行程序将不接受新任务..
    • 请阅读并理解ThreadpoolExecutor的API。不要在无限循环中等待,而是使用awaitTermination。如果您想在同一个Threadpool 中执行多批任务,请不要关闭该池,而是等待getActiveCount == 0 变为真
    猜你喜欢
    • 2011-12-21
    • 2012-01-28
    • 2013-06-27
    • 2011-11-29
    • 2021-12-20
    • 2011-07-15
    • 2014-03-09
    • 2013-06-10
    • 2010-11-05
    相关资源
    最近更新 更多