簡體   English   中英

使用線程池執行任務時出現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()) {

        }

請嘗試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM