美文网首页
ScheduledExecutorService 简单使用

ScheduledExecutorService 简单使用

作者: EvanZch | 来源:发表于2018-11-24 16:06 被阅读6次

简单整理,方便后续查阅

前言

AndroidStudio安装了阿里编码规范插件后,在代码编写的时候,遇到一些不规范的或者需要优化的,都会提示,对编写一些更易阅读和使用的代码还是很有帮助,今天在做延时操作的时候,以前基本使用 Timer,但是阿里编码规范提示 使用 ScheduleExecutorService 代替Timer , 特去查阅了一下 ScheduledExecutorService,顺便整理了一下基本用法,先占坑,后续再详细的去整理。

正文

1、schedule(Runnable command, long delay, TimeUnit unit)

command: 需要执行的任务

delay:任务执行需要延迟的时间

unit:时间单位

2、scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

command: 需要执行的任务

initialDelay:第一次执行延迟的时间

period:间隔周期

unit:时间单位

  • 包含首次延迟的周期性执行任务,第一次执行:delay+period,第二次:delay+2*period,以此类推...
  • 停止:异常停止执行,主动调用停止方法
  • 如果某一个周期执行时间超过设定的period,则后续顺延

3、scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)

command: 需要执行的任务

initialDelay:第一次执行延迟的时间

period:周期之间的延迟,间隔

unit:前两个参数的单位

  • 周期性执行任务:第一次执行:initialDelay+delay,第二次:initialDelay+2*delay,以此类推...
  • 停止:异常停止执行,主动调用停止方法
  • 不顺延

相关文章

网友评论

      本文标题:ScheduledExecutorService 简单使用

      本文链接:https://www.haomeiwen.com/subject/fjlyqqtx.html