-
shutdown()
无法直接停止。
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.schedule(new Runnable() {
@Override
public void run() {
System.out.println("+++ executor run.");
}
}, 5, TimeUnit.SECONDS);
new Thread(new Runnable() {
@Override
public void run() {
HCTools.sleep(1, TimeUnit.SECONDS);
executor.shutdown();
System.out.println("--- executor shutdown.");
}
}).start();
输出结果:
--- executor shutdown.
+++ executor run.
-
shutdownNow()
调用后会立即停止。
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.schedule(new Runnable() {
@Override
public void run() {
System.out.println("+++ executor run.");
}
}, 5, TimeUnit.SECONDS);
new Thread(new Runnable() {
@Override
public void run() {
HCTools.sleep(1, TimeUnit.SECONDS);
executor.shutdownNow();
System.out.println("--- executor shutdownNow.");
}
}).start();
输出结果:
--- executor shutdownNow.