private static final ExecutorService pool = Executors.newFixedThreadPool(threads);
pool.execute(new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(30);
System.out.println(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
pool.shutdown();
pool.awaitTermination(60,TimeUnit.SECONDS);
//关闭之后我 会报错
//Exception in thread "main" java.util.concurrent.RejectedExecutionException
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println(2);
}
});
原因是shutdown之后线程池无法再提交任务。有没有方法让这个线程池再打开呢?
没有,这是
ExecutorService
接口明确规定的,关闭后不允许再打开。