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

所有超级接口:

    ExecutorExecutorService

所有已知实现类:

    ScheduledThreadPoolExecutor

    schedule 方法使用各种延迟创建任务,并返回一个可用于取消或检查执行的任务对象。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 实现提供了便捷的工厂方法。

用法示例

    以下是一个带方法的类,它设置了 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

转载于:https://my.oschina.net/langwanghuangshifu/blog/2963318

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`java.util.concurrent.ScheduledExecutorService`是Java提供的用于执行定时任务的接口。它可以通过线程池来管理任务的执行,并提供了更灵活和功能更强大的定时任务调度机制。下面是一个示例: ```java import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class TaskScheduler { public static void main(String[] args) { ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); Runnable task = new Runnable() { @Override public void run() { // 在这里编写要定时执行的任务逻辑 System.out.println("任务执行中..."); } }; // 延迟1秒后执行任务,之后每隔1秒执行一次 executor.scheduleAtFixedRate(task, 1, 1, TimeUnit.SECONDS); // 等待一段时间后关闭线程池 try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } executor.shutdown(); } } ``` 在上面的示例中,我们使用`Executors.newScheduledThreadPool(int)`方法创建了一个大小为1的线程池,然后通过`executor.scheduleAtFixedRate(Runnable, initialDelay, period, TimeUnit)`方法来安排任务的执行。其中,`Runnable`表示要执行的任务,`initialDelay`表示延迟执行的时间,`period`表示任务的执行间隔,`TimeUnit.SECONDS`表示时间单位为秒。 在`Runnable`的`run()`方法中,编写了要定时执行的任务逻辑。在这个示例中,任务逻辑是简单地输出一条信息。 通过调用`executor.shutdown()`方法,可以手动关闭线程池,停止任务的执行。 需要注意的是,`scheduleAtFixedRate()`方法是按固定的时间间隔执行任务,不受任务执行时间的影响。如果任务执行时间超过了执行间隔,那么下一次任务将会立即开始,而不会等待上一次任务执行完毕。如果希望任务在上一次执行结束后再延迟一段时间再执行,可以使用`scheduleWithFixedDelay()`方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值