首页 > 解决方案 > 线程池执行代码,但在 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:

线程数

标签: javathreadpoolexecutor

解决方案


你必须在下面使用

ExecutorService executorService = Executors.newFixedThreadPool(threadCount);

代替

Executor executor =  Executors.newFixedThreadPool(threadCount);

推荐阅读