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.
*
*This method does not wait for previously submitted tasks to
* complete execution. Use {@link #awaitTermination awaitTermination}
* to do that.
*
上面的意思是说:在先前提交的任务(就是run
中跑的东西)被执行的时候,开始有序的关闭。新的任务不会被执行。如果已关闭,则调用没有其他效果。该方法不会等待先前已提交的任务完全执行。
awaitTermination()
awaitTermination(long timeout, TimeUnit unit)
第一个参数timeout
是等待关闭线程池执行器的时间大小,第二个参数TimeUnit
是时间的粒度,也就是以多大的粒度(小时,分钟,秒)来设置第一个参数的单位。
/**
* Returns true if this executor is in the process of terminating
* after {@link #shutdown} or {@link #shutdownNow} but has not
* completely terminated. This method may be useful for
* debugging.
A return of {@code true} reported a sufficient
* period after shutdown may indicate that submitted tasks have
* ignored or suppressed interruption, causing this executor not
* to properly terminate.
*
* @return {@code true} if terminating but not yet terminated
*/
等待执行器完全关闭,返回为真。
- 总结
shutdown()
关闭任务,awaitTermination
关闭执行器(executor
)。