Gna*_*nam 41 java multithreading keep-alive
继续我发布的问题,我正在尝试在我的代码库中使用ThreadPoolExecutor.即使在多次尝试从Java API doc中理解之后,我也无法清楚地理解keepAliveTime在构造函数中传递的参数背后的功能/目的.希望有人能用一些好的工作实例来解释我.
摘自Java doc:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)
Run Code Online (Sandbox Code Playgroud)
keepAliveTime- 当线程数大于核心数时,这是多余空闲线程在终止之前等待新任务的最长时间.
Jon*_*eet 68
假设您的核心大小为5,最大大小为15.由于某种原因,您的池会变忙,并使用所有15个可用线程.最终你没有工作要做 - 所以你的一些线程在完成最后的任务时就会空闲.因此,其中10个线程被允许死亡.
但是,为了避免它们被快速杀死,您可以指定保持活动时间.所以,如果你指定1作为keepAliveTime值,并TimeUnit.MINUTE为unit值,每个线程将等待一分钟便完成了执行任务,看看是否有更多的工作要做了.如果它还没有得到任何更多的工作,它将自己完成,直到池中只有5个线程 - 池的"核心".
以下是 Javadoc 中的更多描述:
<dt>Keep-alive times</dt>
*
* <dd>If the pool currently has more than corePoolSize threads,
* excess threads will be terminated if they have been idle for more
* than the keepAliveTime (see {@link
* ThreadPoolExecutor#getKeepAliveTime}). This provides a means of
* reducing resource consumption when the pool is not being actively
* used. If the pool becomes more active later, new threads will be
* constructed. This parameter can also be changed dynamically
* using method {@link ThreadPoolExecutor#setKeepAliveTime}. Using
* a value of <tt>Long.MAX_VALUE</tt> {@link TimeUnit#NANOSECONDS}
* effectively disables idle threads from ever terminating prior
* to shut down.
* </dd>
*
Run Code Online (Sandbox Code Playgroud)
本质上,这只是允许您控制空闲池中剩余的线程数量。如果你让它太小(对于你正在做的事情),你将创建太多线程。如果你把它设置得太大,你将消耗不需要的内存/线程。