java.util.concurrent.ScheduledExecutorService 接口 源码

线程池相关

源码:

package java.util.concurrent;

public interface ScheduledExecutorService extends ExecutorService {
    //建立并执行在给定延迟后启用的一次性操做
    public ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit);

    //建立并执行在给定延迟后启用的 ScheduledFuture
    public <V> ScheduledFuture<V> schedule(Callable<V> callable,long delay, TimeUnit unit);

    //建立并执行一个在给定初始延迟后首次启用的按期操做,后续操做具备给定的周期
    //在initialDelay后开始执行,而后以后每隔period执行一次
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit);

    //建立并执行一个在给定初始延迟后首次启用的按期操做,随后在每一次执行终止和下一次执行开始之间都存在给定的延迟
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit);
}

 

接口 ScheduledExecutorService

全部超级接口:html

    ExecutorExecutorServicejava

全部已知实现类:api

    ScheduledThreadPoolExecutor网络

    schedule 方法使用各类延迟建立任务,并返回一个可用于取消或检查执行的任务对象。scheduleAtFixedRate 和 scheduleWithFixedDelay 方法建立并执行某些在取消前一直按期运行的任务。spa

    用 Executor.execute(java.lang.Runnable) 和 ExecutorService 的 submit 方法所提交的命令,经过所请求的 0 延迟进行安排。.net

    schedule 方法中容许出现 0 和负数延迟(但不是周期),并将这些视为一种当即执行的请求。线程

    全部的 schedule 方法都接受相对 延迟和周期做为参数,而不是绝对的时间或日期。将以 Date 所表示的绝对时间转换成要求的形式很容易。code

    例如,要安排在某个之后的 Date 运行,可使用:schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)。htm

    注意,因为网络时间同步协议、时钟漂移或其余因素的存在,所以相对延迟的期满日期没必要与启用任务的当前 Date 相符。 对象

 Executors 类为此包中所提供的 ScheduledExecutorService 实现提供了便捷的工厂方法。

用法示例

    如下是一个带方法的类,它设置了 ScheduledExecutorService ,在 1 小时内每 10 秒钟蜂鸣一次:

package com.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.SECONDS;

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);//建立一个ScheduledFuture 对象实例

        scheduler.schedule(new Runnable() {
            public void run() {
                beeperHandle.cancel(true);//试图取消对此任务的执行。若是任务已完成、或已取消,或者因为某些其余缘由而没法取消,则此尝试将失败
            }
        }, 60 * 60, SECONDS);
    }
}

从接口 java.util.concurrent.ExecutorService 继承的方法

 awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow, submit, submit, submit

 从接口 java.util.concurrent.Executor 继承的方法

 execute

 

schedule

ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit)

    建立并执行在给定延迟后启用的一次性操做。

    参数:

    command - 要执行的任务

    delay - 从如今开始延迟执行的时间

    unit - 延迟参数的时间单位

    返回:

        表示挂起任务完成的 ScheduledFuture,而且其 get() 方法在完成后将返回 null

    抛出:

    RejectedExecutionException - 若是没法安排执行该任务

    NullPointerException - 若是 command 为 null

 

schedule

<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)

    建立并执行在给定延迟后启用的 ScheduledFuture。

    参数:

    callable - 要执行的功能

    delay - 从如今开始延迟执行的时间

    unit - 延迟参数的时间单位

    返回:

        可用于提取结果或取消的 ScheduledFuture

    抛出:

    RejectedExecutionException - 若是没法安排执行该任务

    NullPointerException - 若是 callable 为 null

 

scheduleAtFixedRate

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

 

scheduleWithFixedDelay

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

相关文章
相关标签/搜索