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

首先创建一个带有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();
  • 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部分了解更多。

猜你喜欢

转载自blog.csdn.net/xiaozhude/article/details/83279587
今日推荐