该
shutDown()方法只是防止其他任务被调度。相反,您可以在中调用
shutDownNow()并检查线程是否中断
Runnable。
// in your Runnable...if (Thread.interrupted()) { // Executor has probably asked us to stop}根据您的代码,一个示例可能是:
final ExecutorService executor = Executors.newFixedThreadPool(1);executor.submit(new Runnable() { public void run() { try { Thread.sleep(20 * 1000); } catch (InterruptedException e) { System.out.println("Interrupted, so exiting."); } }});if (executor.awaitTermination(10, TimeUnit.SECONDS)) { System.out.println("task completed");} else { System.out.println("Forcing shutdown..."); executor.shutdownNow();}


