Java 线程中的 RejectedExecutionException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4172674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:10:27  来源:igfitidea点击:

RejectedExecutionException in Java Threads

java

提问by dacwe

I am writing a multithreaded program in java. I have written something like this

我正在用java编写一个多线程程序。我写过这样的东西

exec.execute(p)  // where p is a runnable task working on an array
print array
exec.shutdown

The problem I am facing is that the array gets printed giving the correct output but then the rejected Execution Exception comes I don't understand why when the threads have processed and given the correct output why is the error coming...

我面临的问题是数组被打印,给出了正确的输出,但随后出现了被拒绝的执行异常我不明白为什么当线程已经处理并给出正确的输出时,为什么会出现错误......

回答by dacwe

I think you are shutting down your executor too early. This is an example how I think you should be working.

我认为您过早地关闭了您的 executor。这是我认为你应该如何工作的一个例子。

public class Main {
    public static void main(String[] args) throws Exception {

        // the array to modify
        final int[] array = new int[1000];

        // start the executor (that modifies the array)
        ExecutorService executor = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 1000; i++) {
            final int c = i;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    array[c] = c;
                }
            });
        }

        // wait for all tasks to quit
        executor.shutdown();
        while (!executor.awaitTermination(10, TimeUnit.SECONDS)); 

        // print the array
        System.out.println(Arrays.toString(array));
    }
}

Also note that working on the same array at the same time could cause inconsistency- you must be really sure that you are not doing work on the array that depends on the array.

另请注意,同时在同一阵列上工作可能会导致不一致- 您必须非常确定您没有在依赖于该阵列的阵列上工作。

回答by Mak

Problem is you are still submitting new tasks even after calling shutdown(). So using executor.awaitTermination()won't help.

问题是即使在调用shutdown(). 所以使用executor.awaitTermination()不会有帮助。

To fix the problem, check wheather executor is not shutdown at time of submitting task.

要解决此问题,请检查提交任务时执行程序是否未关闭。

example:

例子:

if (!executor.isShutdown())
{
  executor.execute(new Runnable() {
                         @Override
                         public void run() {
                              array[c] = c;
                         }
                  });
}

Hope it helps ...

希望能帮助到你 ...

回答by Dungeon Hunter

I used to shutdown the executors created in the runtime shutdown hook

我曾经关闭在运行时关闭挂钩中创建的执行程序

Runtime.getRuntime().addShutdownHook(new Thread() {

            public void run() {
            if(!eExecutor.isShutdown()) {
                eExecutor.shutdown();
                // await termination code
              }
            }

        });

回答by Ashkrit Sharma

One more option could be get the future when you submit to executor and then block on future by calling get

当您提交给 executor 时,另一种选择可能是获取未来,然后通过调用 get 阻止未来