最近看了下《java编程思想》中关于并发的小例子,结果发现书上的例子运行不了,很奇怪。是关于CachedTreadPool的。
LiftOff.java
public class LiftOff implements Runnable { protected int countDown = 10; private static int taskCount = 0; private final int id = taskCount++; public LiftOff() { } public LiftOff(int countDown) { this.countDown = countDown; } public String status() { return "#" + id + "(" + (countDown > 0 ? countDown : "Liftoff!") + "),"; } @Override public void run() { while (countDown-- > 0) { System.out.println(status()); Thread.yield(); } } }
CachedTreadPool.java
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CachedTreadPool { public static void main(String[] args) { ExecutorService exec = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) { exec.execute(new LiftOff()); exec.shutdown(); } } }
运行后报错
java.util.concurrent.RejectedExecutionException at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1768) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:767) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:658) at com.test.thread.CachedTreadPool.main(CachedTreadPool.java:13) #0(9), #0(8), #0(7), #0(6), #0(5), #0(4), #0(3), #0(2), #0(1), #0(Liftoff!),
请教各位大侠是什么错