gpt4 book ai didi

java - 如何检查 ExecutorService 是否为 "healthy"并正常工作?

转载 作者:行者123 更新时间:2023-12-02 06:02:47 26 4
gpt4 key购买 nike

我正在为我的公司开发一个无 GUI 的桌面应用程序,计划始终在后台运行,从网站检索信息(通过 HtmlUnit)并更新数据库中的一些行。我正在使用 ExecutorService 提交加载网站的任务,以便我可以设置超时。这样:

private ExecutorService taskExecutor = Executors.newFixedThreadPool(1);
private long timeout = 60000L;

private Page loadSite(Loader<Page> c) {
Page page;
Future<Page> result = taskExecutor.submit(c);
try {
page = result.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException | ExecutionException | InterruptedException ex) {
close();
result.cancel(true);
throw new HandledWebException(ex);
}

return page.getEnclosingWindow().getEnclosedPage();
}

问题是:

  • 我是否应该在 submit 调用之前检查我的 taskExecutor 对象是否能够安排任务,并在必要时重新实例化它? (延长的执行时间对我来说似乎是一种威胁)
  • 如果提交的任务无法完成,我是否必须关闭并重新实例化 taskExecutor

最佳答案

1) 我是否应该检查我的 taskExecutor 对象是否能够在提交调用之前安排任务,并在必要时重新实例化它?

2)如果提交的任务无法完成,我是否必须关闭并重新实例化taskExecutor?

对于这两个问题都不需要

public static ExecutorService newFixedThreadPool(int nThreads)

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

关于java - 如何检查 ExecutorService 是否为 "healthy"并正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55962229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com