Java并发包:ScheduledExecutorService

文章译自:http://tutorials.jenkov.com/java-util-concurrent/index.html
抽空翻译了一下这个教程的文章,后面会陆续放出,如有不妥,请批评指正。
转自请注明出处。

ScheduledExecutorService

java.util.concurrent.ScheduleExecutorService是一种安排任务执行的ExecutorService,任务可以延迟执行,或者在一个固定的时间间隔内重复执行。任务通过工作线程并且不能被正在处理任务的线程异步执行,。

ScheduledExecutorService例子

下面是ScheduledExecutorService的一个例子:

ScheduledExecutorService scheduledExecutorService =
        Executors.newScheduledThreadPool(5);

ScheduledFuture scheduledFuture =
    scheduledExecutorService.schedule(new Callable() {
        public Object call() throws Exception {
            System.out.println("Executed!");
            return "Called!";
        }
    },
    5,
    TimeUnit.SECONDS);

首先创建一个带有5个线程的SechuleExecutorService。然后将Callbale接口的匿名实例类被传递给schedule()方法。最后两个参数指定了Callable将在5秒后执行。

ScheduledExecutorService的具体实现

ScheduledExecutorService是一个接口类,java.util.concurrent包中有以下关于此接口的实现类:

  • ScheduledThreadPoolExecutor
ScheduledExecutorService使用说明

一旦你创建了ScheduledExecutorService的实例,你将会使用下面一些方法:

  • schedule (Callable task, long delay, TimeUnit timeunit)
  • schedule (Runnable task, long delay, TimeUnit timeunit)
  • scheduleAtFixedRate (Runnable, long initialDelay, long period, TimeUnit timeunit)
  • scheduleWithFixedDelay (Runnable, long initialDelay, long period, TimeUnit timeunit)

我将简要说明一下这些方法。

  • schedule (Callable task, long delay, TimeUnit timeunit)

这个方法将在给定的延迟时间后执行Callable。

这个方法返回ScheduledFuture,你可以在任务执行之前使用它来取消任务,如果任务已经执行也可以通过它来得到执行结果。

下面是一个例子:

ScheduledExecutorService scheduledExecutorService =
        Executors.newScheduledThreadPool(5);

ScheduledFuture scheduledFuture =
    scheduledExecutorService.schedule(new Callable() {
        public Object call() throws Exception {
            System.out.println("Executed!");
            return "Called!";
        }
    },
    5,
    TimeUnit.SECONDS);

System.out.println("result = " + scheduledFuture.get());

scheduledExecutorService.shutdown();

例子输出结果:

Executed!
result = Called!
  • schedule (Runnable task, long delay, TimeUnit timeunit)

这个方法类似于用Callable作为参数的版本(上面的方法),ScheduledFuture.get()方法在任务执行完毕时返回null。

  • scheduleAtFixedRate (Runnable, long initialDelay, long period,
    TimeUnit timeunit)

这个方法可以周期性的执行任务。任务在initialDelay时间后第一次执行,然后每次周期循环执行。

如果执行的任务抛出了异常,任务不会再执行了,如果没有抛出异常,任务将继续执行直到 ScheduledExecutorService 关闭。

如果任务执行时间超过了任务之间间隔的时间,下个任务将会在当前任务完成后再执行。执行任务的线程每次不会超过一个。

  • scheduleWithFixedDelay (Runnable, long initialDelay, long period,
    TimeUnit timeunit)

这个方法与scheduleAtFixedRate()方法类似,只是对period理解是不同的。

scheduleAtFixedRate()方法的period指的是上一个任务开始执行到下一个任务开始执行的时间间隔。

然而,这个方法的period指的是上一个任务执行完到下一个任务开始执行之间的时间间隔。

ScheduledExecutorService Shutdown

就像ExecutorService,ScheduleExecutorService在使用完之后需要关闭一样。如果不这样做,jvm将会一直在运行。尽管所有其他的线程都被关闭了。

关闭 ScheduleExecutorService使用shutdown()和shutdownNow()方法,这个两个方法继承自ExecutorService接口。 查看ExecutorService Shutdown部分了解更多。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值