Java并发包:ScheduledExecutorService

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

ScheduledExecutorService

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

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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

ScheduledExecutorService的具体实现

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

  • 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。code

这个方法返回ScheduledFuture,你能够在任务执行以前使用它来取消任务,若是任务已经执行也能够经过它来获得执行结果。htm

下面是一个例子:继承

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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

例子输出结果:

Executed!
result = Called!
  • 1
  • 2
  • 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部分了解更多。

相关文章
相关标签/搜索