shutdown()与awaitTermination()的分析

  • 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)。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值