【发布时间】:2013-10-30 14:13:29
【问题描述】:
有没有办法告诉ExecutorService 在固定线程池场景中关闭空闲线程?
问候!
::编辑: 好吧,我越来越沮丧。我现在已经切换到允许我使用以下构造函数指定 keepAliveTime 的 ThreadPoolExecutor:
private final LinkedBlockingQueue<Runnable> threadPool = new LinkedBlockingQueue<Runnable>()
public Crawler(String startingUrl, int numThreads, int depth) {
System.setProperty("http.agent", "");
// System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, "info");
this.url = startingUrl;
this.maxDepth = depth;
executorService = new ThreadPoolExecutor(
numThreads,
numThreads,
2000,
TimeUnit.MILLISECONDS,
threadPool,
new ThreadPoolExecutor.CallerRunsPolicy());
databaseHandler = new DatabaseHandler();
try {
databaseHandler.init();
} catch (final SQLException e) {
e.printStackTrace();
}
}
// this method is invoked by each Runnable when it found new links
@Override
public void queueLink(String link, int currentDepth) throws Exception {
if (currentDepth < maxDepth) {
final LinkFinder lf = new LinkFinder(link, currentDepth, this);
executorService.execute(lf);
} else {
System.out.println("max depth reached!");
System.out.println("num queued tasks: " + threadPool.size());
}
}
每个新的 Runnable 都会收到一个当前深度属性。在每个 Runnable 的最后一行,currentDepth 加 1。当然“达到最大深度”会按预期打印出来,以及“队列中的任务数:0”。然而,线程继续运行数小时。
我在操作时的印象是参数 keepAliveTime 和 TimeUnit.MILLISECONDS 将确保在两秒钟的空闲时间后,线程被关闭(假设没有其他任务排队)。
我错过了什么?我的意思是,我可以计算我点击 else 语句然后手动关闭执行器的次数,但是由于 ThreadPoolExecutor 已经提供了基于时间的行为,我宁愿坚持下去。
现在我认为这与我制定的政策有关……我会调查一下。还是谢谢
问候!
【问题讨论】:
-
您需要转到 javadoc 进行实施。
-
根据定义,固定大小的池是固定的。如果可能是具有最大大小的缓存池,您真正想要什么?为什么要关闭这些线程?
-
我一直在关注 Madalin Ilie 的教程,该教程通过一个简单的网络爬虫详细说明了 ExecuterService 与 ForkJoinPool 的使用:javaworld.com/javaworld/jw-10-2011/… 我尝试使用最大深度来扩展她的示例以进行爬网。因此,当所有线程都达到最大深度时,它们仍然什么都不做。我现在开始研究 ExecutorService 的 ThreadPoolExecutor 实现,它实现了前面提到的 setKeepAliveTime 方法。我只是想知道我应该使用什么 BlockingQueue。
标签: java multithreading shutdown executorservice