scheduledExecutorService.scheduleWithFixedDelay

scheduledExecutorService.scheduleWithFixedDelay是Java中的一个方法,用于在指定的时间间隔后重复执行一个任务。本文将介绍这个方法的使用方法,并提供相关的代码示例。

1. scheduledExecutorService简介

scheduledExecutorService是Java中的一个接口,它扩展了ExecutorService接口,并提供了一些用于调度任务的方法。通过scheduledExecutorService,我们可以在指定的时间或者时间间隔后执行任务,并且可以根据需求进行取消或者修改已经调度的任务。

2. scheduleWithFixedDelay方法的使用

scheduleWithFixedDelayscheduledExecutorService接口中的一个方法,它的签名如下:

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

该方法接受四个参数:

  • command:一个实现了Runnable接口的任务,将在指定的时间间隔后执行。
  • initialDelay:任务首次执行的延迟时间,以delayunit指定的时间单位为准。
  • delay:两次连续任务执行之间的时间间隔。
  • unit:时间单位,可以是TimeUnit.MILLISECONDSTimeUnit.SECONDS等。

该方法返回一个ScheduledFuture对象,可以用于取消或者查询任务的状态。

下面是一个使用scheduleWithFixedDelay方法的示例代码:

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduleWithFixedDelayExample {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

        Runnable task = () -> {
            System.out.println("Executing task...");
            // 任务的具体逻辑
        };

        // 延迟1秒后执行任务,然后每隔2秒重复执行
        scheduledExecutorService.scheduleWithFixedDelay(task, 1, 2, TimeUnit.SECONDS);
    }
}

在上面的示例中,我们创建了一个ScheduledExecutorService对象,并使用Executors.newScheduledThreadPool方法创建了一个大小为1的线程池。然后,我们定义了一个任务task,在任务中打印了一条信息,并可以编写具体的逻辑。最后,我们使用scheduleWithFixedDelay方法将任务调度,并指定了任务首次执行的延迟时间为1秒,两次连续任务执行之间的时间间隔为2秒。

3. 取消任务和查询任务状态

我们可以使用ScheduledFuture对象来取消或者查询任务的状态。下面是一个示例代码:

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

public class ScheduleWithFixedDelayExample {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

        Runnable task = () -> {
            System.out.println("Executing task...");
            // 任务的具体逻辑
        };

        // 延迟1秒后执行任务,然后每隔2秒重复执行
        ScheduledFuture<?> future = scheduledExecutorService.scheduleWithFixedDelay(task, 1, 2, TimeUnit.SECONDS);

        // 等待5秒后取消任务
        try {
            TimeUnit.SECONDS.sleep(5);
            future.cancel(true);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 查询任务的状态
        boolean isCancelled = future.isCancelled();
        boolean isDone = future.isDone();
        System.out.println("Is cancelled? " + isCancelled);
        System.out.println("Is done? " + isDone);

        // 关闭线程池
        scheduledExecutorService.shutdown();
    }
}

上面的代码示例中,我们通过scheduledExecutorService.scheduleWithFixedDelay方法将任务调度,并将返回的ScheduledFuture对象赋值给future变量。然后,我们等待5秒后取消任务,调用future.cancel(true)方法取消任务,并将true作为参数传递给cancel方法,表示中断正在执行的任务。接着,我们使用future.isCancelled()方法和future.isDone()方法查询任务的状态,并打印出来。最后,我们调用scheduledExecutorService.shutdown()方法关闭线程池。

4. 结语

本文介绍了scheduledExecutorService.scheduleWithFixedDelay方法的使用方法,并提供了相关的代码示例。