【问题标题】:RejectedExecutionException when executing tasks using threadpool : JAVA使用线程池执行任务时出现 RejectedExecutionException:JAVA
【发布时间】:2017-04-28 02:51:29
【问题描述】:

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

这是我的代码 sn-p

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();
        }
    }

【问题讨论】:

    标签: java threadpool threadpoolexecutor


    【解决方案1】:

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

    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()) {
    
            }
    

    请尝试一下

    【讨论】:

      猜你喜欢
      • 2015-06-26
      • 1970-01-01
      • 2013-07-22
      • 1970-01-01
      • 2019-10-08
      • 2011-11-03
      • 2016-06-24
      • 1970-01-01
      • 2015-04-24
      相关资源
      最近更新 更多