使用线程池执行任务时出现 RejectedExecutionException:JAVA

RejectedExecutionException when executing tasks using threadpool : JAVA

线程池在提交时拒绝任务。线程池大小是固定的,它是 8。即使我没有提交超过 8 个任务,它也会拒绝。我尝试使用阻塞队列,但它对我没有帮助。

这是我的代码片段

try {
            List<Future> tasks = new ArrayList<Future>();
            ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
            Process process = new Process();
            ProcessingJobMeta meta = process.getPJM();
            List<CuratedInput> cil = meta.getCuratedInputList();
            for (final CuratedInput ci : cil) {
                for (final Preperation prep : Preperation.values()) {
                    for (final Export export : Export.values()) {
                        Runnable runnable = new Runnable() {
                            public void run() {
                                LOGGER.info("Executing.................." + prep.toString() );
                                LOGGER.info("Executing.................." + export.toString());
                                PreperationFactory.getPreperation(prep.toString(), ci);
                                ExportFactory.getExport(export.toString(), ci);
                            }
                        };
//                      tpe.submit(runnable);
                        tasks.add((Future) tpe.submit(runnable));

                        for (Future p : tasks) {
                            LOGGER.info("---------------inside the futures for loop------------");
                            LOGGER.info("Result of the future executed ------> " + p.get());
                        }

                        tpe.shutdown();

                        while (!tpe.isShutdown()) {

                        }
                    }
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

您的问题是您在循环中关闭池并试图向已关闭的池中添加更多线程。将这些行放在循环之外

tpe.shutdown();

while (!tpe.isShutdown()) {

}

像这样

List<Future> tasks = new ArrayList<Future>();
        ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
        Process process = new Process();
        ProcessingJobMeta meta = process.getPJM();
        List<CuratedInput> cil = meta.getCuratedInputList();
        for (final CuratedInput ci : cil) {
            .....
        }

        tpe.shutdown();
        while (!tpe.isShutdown()) {

        }

请试试看