我在一个do while循环中使用ThreadPool执行器作为守护进程。在每次迭代开始时,我创建newFixedThreadPool: executor = Executors.newFixedThreadPool(5);
任务完成后,我会在下一次迭代开始之前调用shutdown,并为新的5个工作线程创建newFixedPool。
现在,在打印Log4J日志时。我使用%t来显示正在执行哪个线程池。我看到每一次迭代都会创建一个递增的新线程池。
在开始下一次迭代之前,请让我知道我需要调用executor=null吗?
是递增的线程池指示,线程池正在累积,可能会导致内存泄漏,因为它是一个守护进程,或者在后端线程池中将被垃圾收集,因为所有线程都已经完成,并且我们已经执行了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发布于 2014-06-26 17:45:25
不要关闭你的线程池,而是重新使用它。这就是拥有线程池的全部意义:您将任务提交到这个池中,而不必关心线程管理。只需等待线程终止并将下一批任务提交到同一池中即可。
https://stackoverflow.com/questions/24427177
复制相似问题