我在do while循环中使用ThreadPool执行器作为deamon进程。 在每次迭代开始时,我都在创建newFixedThreadPool: executor = Executors.newFixedThreadPool(5);
任务完成后,我在下一次迭代开始之前调用shutdown,并为新的5个工作线程创建newFixedPool。
现在打印Log4J日志。我使用[%t]来显示执行哪个线程池。 我看到每次迭代都会使用递增的数字创建新的线程池。
请在开始下一次迭代之前告诉我是否需要调用executor = null?
增加的线程池是否指示,线程池是否已累积并可能导致内存泄漏,因为它是一个deamon进程或后端线程池将被垃圾收集,因为所有线程都已完成且我们已经执行了shutdown。
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
答案 0 :(得分:0)
不要关闭Threadpool但重新使用它。这是拥有Threadpool的重点:您将任务提交到此池中,而不必关心线程管理。只需等待你的线程终止并将下一批任务提交到同一个池中。