在这篇文章中,我们将学习如何使用ScheduledExecutorService接口将命令安排在给定的延迟后运行,或定期执行。
一个ScheduledExecutorService
可以安排命令在给定的延迟后运行或定期执行。
schedule()
方法创建具有各种延迟的任务,并返回一个可以用来取消或检查执行的任务对象。scheduleAtFixedRate()
和scheduleWithFixedDelay()
方法创建并执行定期运行的任务,直到取消为止。
使用Executor.execute(Runnable)
和ExecutorService
提交方法提交的命令以要求的延迟为零来安排。在计划方法中也允许零和负延迟(但不是周期),并被视为立即执行的请求。
public interface ScheduledExecutorService extends ExecutorService {
public ScheduledFuture<?> schedule(Runnable command,
long delay, TimeUnit unit);
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
long delay, TimeUnit unit);
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
}
ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit)
- 创建并执行一个ScheduledFuture,在给定的延迟后变为启用。ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
- 创建并执行一个一次性动作,在给定的延迟后变为有效。ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
- 创建并执行一个周期性动作,该动作首先在给定的初始延迟后启用,然后在给定的周期后启用;即执行将在初始延迟后开始,然后是初始延迟+周期,然后是初始延迟+2/*周期,以此类推。ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
- 创建并执行一个周期性动作,该动作首先在给定的初始延迟后启用,随后在一个执行的终止和下一个执行的开始之间有给定延迟。让我们通过一个例子来理解ScheduledExecutorService
接口的方法。
public class SchedulingTasksWithScheduledThreadPool {
public static void main(String[] args) throws InterruptedException {
System.out.println("Thread main started");
// Create a task
Runnable task1 = () -> {
System.out.println("Executing the task1 at: " + new Date());
};
// Create a task
Runnable task2 = () -> {
System.out.println("Executing the task2 at: " + new Date());
};
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
System.out.println("Scheduling task to run after 5 seconds... " + new Date());
scheduledExecutorService.schedule(task1, 5, TimeUnit.SECONDS);
scheduledExecutorService.schedule(task2, 5, TimeUnit.SECONDS);
scheduledExecutorService.shutdown();
System.out.println("Thread main finished");
}
}
输出:
Thread main started
Scheduling task to run after 5 seconds... Sat Sep 01 10:56:40 IST 2018
Thread main finished
Executing the task1 at: Sat Sep 01 10:56:45 IST 2018
Executing the task2 at: Sat Sep 01 10:56:45 IST 2018
*scheduledExecutorService.schedule()*函数接收一个Runnable,一个延迟值,以及延迟的单位。上面的程序在提交后的5秒后执行任务。
现在让我们看看一个例子,我们定期执行任务 --
public class SchedulingTasksWithScheduledThreadPool {
public static void main(String[] args) throws InterruptedException {
System.out.println("Thread main started");
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
// Create a task
Runnable task1 = () -> {
System.out.println("Executing the task1 at: " + new Date());
};
scheduledExecutorService.scheduleAtFixedRate(task1, 0, 2, TimeUnit.SECONDS);
System.out.println("Thread main finished");
}
}
输出:
Thread main started
Thread main finished
Executing the task1 at: Sat Sep 01 11:03:16 IST 2018
Executing the task1 at: Sat Sep 01 11:03:18 IST 2018
Executing the task1 at: Sat Sep 01 11:03:20 IST 2018
Executing the task1 at: Sat Sep 01 11:03:22 IST 2018
Executing the task1 at: Sat Sep 01 11:03:24 IST 2018
......
注意,如果任务遇到异常,任务的后续执行会被抑制。否则,只有当你关闭执行器或杀死程序时,该任务才会终止。
###参考
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledExecutorService.html
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/09/scheduledexecutorservice-interface-in-java.html
内容来源于网络,如有侵权,请联系作者删除!