[英]Why do threads through a loop give an error when with Executor
The sample code is taken from the Java Philosophy 2015 book and it used Java SE5/6.示例代码取自 Java Philosophy 2015 一书,它使用了 Java SE5/6。 I used JDK11, maybe this code is not suitable for the new version, but why?
我用的是JDK11,可能这段代码不适合新版本,但为什么呢?
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
for(int i=0;i<5;i++) {
exec.execute(new LiftOff2());
exec.shutdown();
}
}
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task LiftOff2@5f205aa rejected from java.util.concurrent.ThreadPoolExecutor@6d86b085[Shutting down, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
at java.base/java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2055)
at java.base/java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:825)
at java.base/java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1355)
at Runnable_p897.main(Runnable_p897.java:8)
From the documentation of ExecutorService::shutdown
:从
ExecutorService::shutdown
的文档:
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted .
启动有序关闭,其中执行先前提交的任务,但不会接受新任务。 ...
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.