shutdown()
/**
* Initiates an orderly shutdown in which previously submitted
* tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
*
* <p>This method does not wait for previously submitted tasks to
* complete execution. Use {@link #awaitTermination awaitTermination}
* to do that.
*
* @throws SecurityException {@inheritDoc}
*/
整体意思:
- 启动之前提交的有序关闭执行任务,但不会接受新的任务。如果线程池已经shutdown了,但是剩下的任务不会受到影响
- 此方法不等待以前提交的任务完全执行。使用{@link#awaitTermination awaitTermination}这样做
shutdownNow()
/**
* Attempts to stop all actively executing tasks, halts the
* processing of waiting tasks, and returns a list of the tasks
* that were awaiting execution. These tasks are drained (removed)
* from the task queue upon return from this method.
*
* <p>This method does not wait for actively executing tasks to
* terminate. Use {@link #awaitTermination awaitTermination} to
* do that.
*
* <p>There are no guarantees beyond best-effort attempts to stop
* processing actively executing tasks. This implementation
* cancels tasks via {@link Thread#interrupt}, so any task that
* fails to respond to interrupts may never terminate.
*
* @throws SecurityException {@inheritDoc}
*/
整体意思:
- 尝试停止所有正在运行的任务,停止等待队列中的任务;清空等待队列,并将队列中的任务返回
- 该方法不会等待正在进行的任务去停止,如果需要此需求,使用awaitTermination方法
- 除了尽最大努力之外,没有其他保证能确保正在进行的任务会停止;采取的停止手段就是使用线程中断,所以如果任务没有对线程中断进行回应则有可能永远不会停止
总结:
shutdownNow,不能保证当前正在运行的任务被停止;
shutdown会将当前运行的任务与等待队列中的任务均执行结束,而shutdownNow会抛弃等待队列中的任务;
两者均不接收新的任务。
测试用例
public class Test16 {
public static ThreadPoolExecutor FullSynExecutor = null;
public static void main(String[] args) throws IOException {
if (FullSynExecutor == null) { // 懒加载线程池
FullSynExecutor = new ThreadPoolExecutor(4, 10, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.AbortPolicy());
FullSynExecutor.allowCoreThreadTimeOut(true);
}
MyRunner test1 = new MyRunner("test1");
MyRunner test2 = new MyRunner("test2");
new Thread(test1).start();
new Thread(test2).start();
new Thread(new MyRunner("test3")).start();
new Thread(new MyRunner("test4")).start();
new Thread(new MyRunner("test5")).start();
new Thread(new MyRunner("test6")).start();
}
static class MyRunner implements Runnable{
private String name;
public MyRunner(String name){
this.name = name;
}
@Override
public void run() {
CompletableFuture.runAsync(() -> {
System.out.println(name + " start!");
int i = 0;
for(;i<200000; ++i){
// 如果需要在shutdown或者shutdownnow时,停止正在运行的任务,可以手动响应中断
/* if(Thread.currentThread().isInterrupted()){
System.out.println(name + " interrupted "+ i );
return;
}*/
// if( i % 5000 == 0)
// System.out.println(i + name);
if(i == 10500){
// 方法1
// FullSynExecutor.shutdown();
// 方法2
List<Runnable> runnables = FullSynExecutor.shutdownNow();
for (Runnable runnable: runnables){
runnable.run();
}
// 方法3
/* try {
FullSynExecutor.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
System.out.println(name +" shutdownnow");
}
}
}, FullSynExecutor).whenComplete((o1, o2) -> {
System.out.println(name + " complete!");
});
}
}
}