Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 如果仅使用声明初始化,ExecutorService将抛出RejectedExecutionException_Multithreading_Exception_Scheduledexecutorservice - Fatal编程技术网

Multithreading 如果仅使用声明初始化,ExecutorService将抛出RejectedExecutionException

Multithreading 如果仅使用声明初始化,ExecutorService将抛出RejectedExecutionException,multithreading,exception,scheduledexecutorservice,Multithreading,Exception,Scheduledexecutorservice,我得到了一些代码,如果我只使用初始化声明,如 private static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); 这将使我打开一个java.util.concurrent.RejectedExecutionException executor.scheduleWithFixedDelay(runnable, 0, 2000, TimeUnit.MILLISECONDS)

我得到了一些代码,如果我只使用初始化声明,如

private static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
这将使我打开一个
java.util.concurrent.RejectedExecutionException

executor.scheduleWithFixedDelay(runnable, 0, 2000, TimeUnit.MILLISECONDS);
但是如果我之前再次初始化

executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleWithFixedDelay(runnable, 0, 2000, TimeUnit.MILLISECONDS);

一切正常。为什么呢?我发现的所有示例并不是每次在调度之前都执行SingleThreadExecutor。我以为这只是设定游泳池的大小。在申报过程中它应该被初始化了?!我很困惑:)Thank

如果在您尝试提交新作业以使其运行时被关闭,
执行器将抛出RejectedExecutionException。您的
executor
变量被声明为
static
,这意味着类的所有实例将共享同一个变量。是否有可能您的类的一个实例正在关闭executor,然后该类的另一个实例正在尝试安排新作业?

两件事

  • 避免声明任何非最终的静态内容
  • 它可能在某个时候关闭了

  • 不管怎样,这是一次关机--当没有任务运行时,我希望这是真的--谢谢你为我浪费时间:p

    在这个脚本中没有关机,任务应该无限期地运行,直到应用程序关机:/i我想除非我们看到更多的代码,否则真的不可能知道。完整的stacktrace也会有帮助上面的代码基本上就是它,我甚至没有把关机放进去,我只是在测试基本的行为:p,但我用相同的执行器调用初始化相同的变量。。。它只在我开始计划之前起作用。