【问题标题】:Is it good to run Http Server or infinite duration tasks in ExecutorService thread [closed]在 ExecutorService 线程中运行 Http Server 或无限持续时间的任务是否好[关闭]
【发布时间】:2019-01-08 07:58:13
【问题描述】:

我有一个无限运行多个任务的 java 应用程序。假设任务 FileRead 反复读取文件并尝试查找新信息并进行处理。第二个任务是http服务器。我正在使用 ExecutorService 在单独的线程中运行这两个任务。

ExecutorService threadPool = Executors.newFixedThreadPool(2, factory);
threadPool.execute(new FileRead());
threadPool.execute(new JettyServer(port));

这个sn-p有什么问题吗?

【问题讨论】:

标签: java executorservice java-threads


【解决方案1】:

是的,让池为您管理资源,来自doc

线程池解决了两个不同的问题:由于减少了每个任务的调用开销,它们通常在执行大量异步任务时提供改进的性能,它们提供了一种限制和管理资源(包括线程)的方法,执行任务集合时消耗。每个ThreadPoolExecutor还维护着一些基本的统计数据,比如c的数量

【讨论】:

  • threadpool.shutDown(); threadpool.awaitTermination() 怎么样?在这种情况下使用它们是否可取?由于这些是无限线程,awaitTermination 将中断进程。
猜你喜欢
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多