且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Keep-alive如何与ThreadPoolExecutor一起工作?

更新时间:2023-09-14 14:09:22

假设您的核心大小为5,最大大小为15.由于某种原因,您的池变得很忙,并使用了所有15个可用线程.最终,您会耗尽工作量,因此某些线程在完成最终任务时会变得空闲.因此,其中10个线程被允许死亡.

Suppose you have a core size of 5, and a maximum size of 15. For some reason your pool gets busy, and uses all 15 available threads. Eventually you run out of work to do - so some of your threads become idle as they finish their final task. So 10 of those threads are allowed to die.

但是,为避免过快地杀死它们,可以指定保持活动时间.因此,如果将1指定为keepAliveTime值并将TimeUnit.MINUTE指定为unit值,则每个线程在完成执行任务后将等待一分钟,以查看是否还有更多工作要做.如果仍然没有做更多的工作,它将自行完成,直到池中只有5个线程-池的核心".

However, to avoid them being killed off too quickly, you can specify the keep-alive time. So if you specified 1 as the keepAliveTime value and TimeUnit.MINUTE as the unit value, each thread would wait one minute after it had finished executing a task to see if there was more work to do. If it still hadn't been given any more work, it would let itself complete, until there were only 5 threads in the pool - the "core" of the pool.