为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

ScheduledExecutorService 定时任务

2017-09-25 5页 doc 19KB 14阅读

用户头像

is_954223

暂无简介

举报
ScheduledExecutorService 定时任务ScheduledExecutorService 定时任务 ScheduledExecutorService 2010-10-05 16:48:14| 分类: JAVA执行器 | 标签: |字号大中小 订阅 ScheduledExecutorService 所有超级接口: Executor, ExecutorService 所有已知实现类: ScheduledThreadPoolExecutor public interface ScheduledExecutorService extends Executor...
ScheduledExecutorService 定时任务
ScheduledExecutorService 定时任务 ScheduledExecutorService 2010-10-05 16:48:14| 分类: JAVA执行器 | 标签: |字号大中小 订阅 ScheduledExecutorService 所有超级接口: Executor, ExecutorService 所有已知实现类: ScheduledThreadPoolExecutor public interface ScheduledExecutorService extends ExecutorService ScheduledExecutorService是一个 ExecutorService,但是可安排在给定的延迟后运行或定期执行的命令。 schedule方法使用各种延迟创建任务,并返回一个可用于取消或检查执行的任务的ScheduledFuture对象。 scheduleAtFixedRate 和 scheduleWithFixedDelay 方法创建并执行某些在取消前一直定期运行的任务。 用 Executor.execute(java.lang.Runnable) 和 ExecutorService 的 submit 方法所提交的命令,通过所请求的 0 延迟进行安排。 schedule方法中允许出现 0 和负数延迟(但不是周期),并将这些视为是立即执行的请求。 所有的 schedule 方法都接受相对延迟和周期作为参数,而不是绝对的时间或日期。 将以 Date 所示的绝对时间转换成的形式很容易。例如,要安排在某个以后的 Date 运行, 可以使用:schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)。 但是要注意,由于网络时间同步、时钟漂移或其他因素的存在,因此相对延迟的期满日期不必与启用任务的当前 Date 相符。 Executors 类为此包中所提供的 ScheduledExecutorService 实现提供了便捷的工厂方法。 注意1:它只有一个实现类ScheduledThreadPoolExecutor。 关于ScheduledThreadPoolExecutor的更多内容请参考《ScheduledThreadPoolExecutor》 用法示例 以下是一个带方法的类,它设置了 ScheduledExecutorService ,在 1 小时内每 10 秒钟蜂鸣一次: import static java.util.concurrent.TimeUnit.*; class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void beepForAnHour() { final Runnable beeper = new Runnable() { public void run() { System.out.println("beep"); }; final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 60 * 60, SECONDS); } }} 主要方法: ScheduledFuture schedule(Runnable command, long delay, TimeUnit unit) 创建并执行在给定延迟后启用的一次性操作。 参数: command - 要执行的任务 delay - 从现在开始延迟执行的时间 unit - 延迟参数的时间单位 返回: 表示挂起任务完成的 ScheduledFuture,并且其 get() 方法在完成后将返回 null 抛出: RejectedExecutionException - 如果无法安排执行该任务 NullPointerException - 如果 command 为 null ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit) 创建并执行在给定延迟后启用的 ScheduledFuture。 参数: callable - 要执行的功能 delay - 从现在开始延迟执行的时间 unit - 延迟参数的时间单位 返回: 可用于提取结果或取消的 ScheduledFuture 抛出: RejectedExecutionException - 如果无法安排执行该任务 NullPointerException - 如果 callable 为 null ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后 在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。如果任务的任何一个执行遇到异常,则后续执行都会被取消。 否则,只能通过执行程序的取消或终止方法来终止该任务。如果此任务的任何一个执行要花费比其周期更长的时间,则将推迟后续执行,但不会同 时执行。 参数: command - 要执行的任务 initialDelay - 首次执行的延迟时间 period - 连续执行之间的周期 unit - initialDelay 和 period 参数的时间单位 返回: 表示挂起任务完成的 ScheduledFuture,并且其 get() 方法在取消后将抛出异常 抛出: RejectedExecutionException - 如果无法安排执行该任务 NullPointerException - 如果 command 为 null IllegalArgumentException - 如果 period 小于等于 0 ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) 创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。 如果任务的任一执行遇到异常,就会取消后续执行。否则,只能通过执行程序的取消或终止方法来终止该任务。 参数: command - 要执行的任务 initialDelay - 首次执行的延迟时间 delay - 一次执行终止和下一次执行开始之间的延迟 unit - initialDelay 和 delay 参数的时间单位 返回: 表示挂起任务完成的 ScheduledFuture,并且其 get() 方法在取消后将抛出异常 抛出: RejectedExecutionException - 如果无法安排执行该任务 NullPointerException - 如果 command 为 null。 IllegalArgumentException - 如果 delay 小于等于 0
/
本文档为【ScheduledExecutorService 定时任务】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索