java - 线程池执行代码,但在 IDE 内容辅助中没有关闭选项
问题描述
我正在尝试在为每个线程分配参数的应用程序中执行 concurrent.Execute 线程池。我在处理过程中发现,创建的线程似乎比预期的要多。也没有关闭选项可用。也许下面的代码会更好地说明这个问题?
这是每个线程最终到达的方法,Arraylist 是在前面的方法中创建的。线程创建如下:
Executor executor =
Executors.newFixedThreadPool(threadCount);
for (final String server : serverList) {
executor.execute(new Runnable() {
@Override
public void run() {
while
(!Thread.currentThread().isInterrupted()) {
try {
...
retrieveMetrics(filterItem,pserver,assignedServer,
Bean);
在 while 循环结束时,我尝试实现 executor.shutdown() 但它不是定义的方法。我上面的实现也是正确的,实际上只允许创建有限数量的线程。在这里欣赏建议和回应。
编辑:
由于某种原因,我无法使用 shutdown()。
编辑2:
解决方案
你必须在下面使用
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
代替
Executor executor = Executors.newFixedThreadPool(threadCount);
推荐阅读
- firebase - Firestore 侦听器未按预期工作
- node.js - electron fluent-ffmpeg mergeToFile() 命令承诺不会触发
- javascript - 展开时沿树视图添加垂直线
- ios - 你知道怎么做这样的虚线吗
- octobercms - 带有 SEO 插件的 OctoberCMS 翻译插件不会翻译 CMS 页面和博客的 SEO 关键字
- ajax - Laravel 7 中的 ajax()
- python - 由 FreqDist 和 ngrams 创建的键中的 remove ()
- c++ - 在 C++ 中使用节点获得一个奇怪的值
- python - ValueError:两个形状中的维度 1 必须相等,但为 299 和 3。形状为 [?,299,299,3] 和 [1,3,299,299]
- python - 解析一个用引号括起来的字符串,其中包含单词“...”并打印字符串 Python Regex