一、简介

An ExecutorService that can schedule commands to run after a given delay, or to execute periodically.

(ExecutorService可以安排命令在给定的延迟后运行或定期执行。)

The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution. The scheduleAtFixedRate and scheduleWithFixedDelay methods create and execute tasks that run periodically until cancelled.

(调度方法会创建具有各种延迟的任务,并返回可用于取消或检查执行的任务对象。 scheduleAtFixedRate和scheduleWithFixedDelay方法创建并执行定期运行的任务,直到被取消为止。

)

Commands submitted using the Executor.execute(java.lang.Runnable) and ExecutorService submit methods are scheduled with a requested delay of zero. Zero and negative delays (but not periods) are also allowed in schedule methods, and are treated as requests for immediate execution.

(使用Executor.execute(java.lang.Runnable)和ExecutorService提交方法提交的命令的计划延迟为零。调度方法中还允许零延迟和负延迟(但不允许使用周期),并将其视为立即执行的请求。

)

All schedule methods accept relative delays and periods as arguments, not absolute times or dates. It is a simple matter to transform an absolute time represented as a Date to the required form. For example, to schedule at a certain future date, you can use: schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS). Beware however that expiration of a relative delay need not coincide with the current Date at which the task is enabled due to network time synchronization protocols, clock drift, or other factors. The Executors class provides convenient factory methods for the ScheduledExecutorService implementations provided in this package.

(所有调度方法都接受相对延迟和周期作为参数,而不是绝对时间或日期作为参数。将代表日期的绝对时间转换为所需的形式很简单。例如,要计划在某个将来的日期进行计划,可以使用:schedule(task,date.getTime()-System.currentTimeMillis(),TimeUnit.MILLISECONDS)。但是请注意,由于网络时间同步协议,时钟漂移或其他因素,相对延迟的到期时间不必与启用任务的当前日期一致。 Executors类为此程序包中提供的ScheduledExecutorService实现提供了方便的工厂方法。

)

(jdk7 doc , translate by google )

二、两个常用定时任务

1. 固定周期的定时任务

ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

创建并执行一个周期性操作,该操作将在给定的初始延迟后首先启用,然后在给定的时间段内启用;即后执行将开始 在initialDelay然后在initialDelay +周期,然后 在initialDelay + 2 *周期,依此类推。

2. 固定延迟的定时任务

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

创建并执行一个周期性操作,该操作将在给定的初始延迟后首先启用,然后在一个执行的终止与下一个执行的开始之间具有给定的延迟。

3. 一次性延迟任务 Runnable

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

创建并执行一次操作,该操作在给定的延迟后变为启用状态。

ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
System.out.println("一次性的延迟任务, 10S 后执行");
executorService.schedule(new Runnable() {
@Override
public void run() {
System.out.println("一次性延迟任务");
}
}, 10L, SECONDS);

4. 一次性延迟任务 Callable

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

创建并执行ScheduledFuture,该ScheduledFuture在给定的延迟后变为启用状态。

三、示例代码

class PrintControl {
private final SimpleDateFormat SDF = new SimpleDateFormat("hh:mm:ss"); /**
* @param int corePoolSize 线程池中最小的线程数, 无任务时保持, 任务过多时可以超出这个值
*/
private final ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1); /**
* 给定延迟的打印任务
* 以固定的延迟时间(delay)去执行任务
*/
public void printForDelay() {
Runnable print = () -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("ScheduledWithFixedDelay" + SDF.format(new Date()));
}; /**
* @param Runnable command
* @param long initialDelay
* @param long delay
* @param TimeUnit unit
*/
scheduled.scheduleWithFixedDelay(print, 0L, 5L, SECONDS);
} /**
* 定期去执行打印任务
* 以固定的周期(period)去执行任务
*/
public void printForRate() {
Runnable print = () -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("ScheduledAtFixedRate" + SDF.format(new Date()));
}; /**
* @param Runnable command
* @param long initialDelay
* @param long period
* @param TimeUnit unit
*/
scheduled.scheduleAtFixedRate(print, 0L, 5L, SECONDS);
} }

四、通过ThreadFactory 指定任务线程名称

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)

使用ThreadFactory设置明确的线程名称, 这样在调试的时候就可以很清晰的找到任务线程, 便于调试

参考 Executors.defaultThreadFactory()

class PrintControl {
private final ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1, ExecutorsefaultThreadFactory());
}
class PrintThreadFactory implements ThreadFactory {

    @Override
public Thread newThread(Runnable r) {
return new Thread(r, "PrintThreadFactory");
}
}

五、结束任务

1. 异常抛出

如果任务抛出异常会导致周期任务无法继续进行, 所以最好结合try cache处理.

此时线程不会结束.

final AtomicInteger count = new AtomicInteger(0);

ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1, new PrintThreadFactory());

StringBuilder nullPoint = null;

Runnable print = new Runnable() {
@Override
public void run() {
System.out.println("print " + count.getAndIncrement());
// print 4 之后, 任务不再继续执行
if (count.get() == 5) {
nullPoint.toString();
}
}
}; schedule.scheduleAtFixedRate(print, 0L, 2L, SECONDS);

2. 结束任务

Future.cancle(boolean mayInterruptIfRunning)

通过Debugger可以看出, 只是任务结束了,线程还在继续wait

// 最终打印 "print 3"之后打印"is canclelled", 任务不再继续, 线程不被关闭

final AtomicInteger count = new AtomicInteger(0);
final CountDownLatch countDownLatch = new CountDownLatch(1);
Future future = null;
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1, new PrintThreadFactory()); Runnable print = new Runnable() {
@Override
public void run() {
System.out.println("print " + count.getAndIncrement());
if (count.get() == 3) {
countDownLatch.countDown();
}
}
}; future = schedule.scheduleAtFixedRate(print, 0L, 2L, SECONDS);
try {
countDownLatch.await();
future.cancel(true);
if (future.isCancelled()) {
System.out.println("is canclelled");
}
} catch (InterruptedException e) {
e.printStackTrace();
}

3. 结束线程

3.1. void ExecutorService.shutdown();

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

(启动有序关闭,在该关闭中执行先前提交的任务,但不接受任何新任务。)

final AtomicInteger count = new AtomicInteger(0);
final CountDownLatch countDownLatch = new CountDownLatch(1);
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1, new PrintThreadFactory()); Runnable print = new Runnable() {
@Override
public void run() {
System.out.println("print " + count.getAndIncrement());
if (count.get() == 3) {
countDownLatch.countDown();
System.out.println("任务还在继续...");
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("任务结束");
}
}
}; schedule.scheduleAtFixedRate(print, 0L, 2L, SECONDS);
try {
countDownLatch.await();
schedule.shutdown();
// schedule.shutdownNow(); if (schedule.isShutdown()) {
System.out.println("Schedule is shutdown");
}
// 阻塞
if (schedule.awaitTermination(10L, SECONDS)) {
System.out.println("termination");
}
} catch (InterruptedException e) {
e.printStackTrace();
}

打印结果:

print 0
print 1
print 2
任务还在继续...
Schedule is shutdown
任务结束
termination

3.2. List ExecutorService.shutdownNow();

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

(尝试停止所有正在执行的任务,暂停正在等待的任务的处理,并返回正在等待执行的任务的列表。)

final AtomicInteger count = new AtomicInteger(0);
final CountDownLatch countDownLatch = new CountDownLatch(1);
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1, new PrintThreadFactory()); Runnable print = new Runnable() {
@Override
public void run() {
System.out.println("print " + count.getAndIncrement());
if (count.get() == 3) {
countDownLatch.countDown();
System.out.println("任务还在继续...");
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("任务结束");
}
}
}; schedule.scheduleAtFixedRate(print, 0L, 2L, SECONDS);
try {
countDownLatch.await();
// schedule.shutdown();
schedule.shutdownNow(); if (schedule.isShutdown()) {
System.out.println("Schedule is shutdown");
}
// 阻塞
if (schedule.awaitTermination(10L, SECONDS)) {
System.out.println("termination");
}
} catch (InterruptedException e) {
e.printStackTrace();
}

打印结果: 可以看出 Thread.sleep()发生了中断异常

print 0
print 1
print 2
任务还在继续...
Schedule is shutdown
java.lang.InterruptedException: sleep interrupted
...
任务结束
termination

3.3. boolean ExecutorService.awaitTermination(long timeout, TimeUnit unit)

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

(阻塞直到关闭请求后所有任务完成执行,或者发生超时,或者当前线程被中断(以先发生的为准)。)

六、参考

Executors框架之ScheduledExecutorService实现定时任务的更多相关文章

  1. javade多任务处理之Executors框架(线程池)实现的内置几种方式与两种基本自定义方式

    一 Executors框架(线程池) 主要是解决开发人员进行线程的有效控制,原理可以看jdk源码,主要是由java.uitl.concurrent.ThreadPoolExecutor类实现的,这里只 ...

  2. 什么是 Executors 框架?

    Executor 框架是一个根据一组执行策略调用,调度,执行和控制的异步任务的框 架. 无限制的创建线程会引起应用程序内存溢出.所以创建一个线程池是个更好的的 解决方案,因为可以限制线程的数量并且可以 ...

  3. java多线程系列:Executors框架

    目录 Executor接口介绍 ExecutorService常用接口介绍 创建线程池的一些方法介绍 3.1 newFixedThreadPool方法 3.2 newCachedThreadPool方 ...

  4. 基于spring的quartz定时框架,实现简单的定时任务功能

    在项目中,经常会用到定时任务,这就需要使用quartz框架去进行操作. 今天就把我最近做的个人主页项目里面的定时刷新功能分享一下,很简单. 首先需要配置一个配置文件,因为我是基于spring框架的,所 ...

  5. Java之ssh框架spring配置文件配置定时任务

    最近做了一个数据同步功能,要求晚上0点去定时同步数据,这是个老项目框架用的ssh,定时任务基于quartz,废话不多说,下面详细说说相关配置. 在spring的配置文件中: <!-- 0点定时任 ...

  6. Spring框架中的Quartz定时任务使用笔记(通过@Scheduled注解的方式实现)

    1.修改spring的xml配置信息 applicationContext.xml 三个部分内容 1.xmlns添加:xmlns:task="http://www.springframewo ...

  7. Spring框架最简单的定时任务调用

    package org.jeecgframework.core.timer; import org.springframework.scheduling.annotation.Scheduled; i ...

  8. SpringMVC框架使用注解执行定时任务(转)

    首先要配置我们的SpringMVC文件 xmlns 加下面的内容: xmlns:task="http://www.springframework.org/schema/task" ...

  9. <线程池-定时任务> ScheduledExecutorService之shutdown引发的RejectedExecutionException问题

    一. 问题描述 先来看一下异常信息,启动tomcat时就报错: 2015-3-20 15:22:39 org.apache.catalina.core.StandardContext listener ...

随机推荐

  1. 曹工说Spring Boot源码(29)-- Spring 解决循环依赖为什么使用三级缓存,而不是二级缓存

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  2. 实现一个字符串匹配算法,从字符串 H 中,查找 是否存在字符串 Y ,若是存在返回所在位置的索引,不存在返回 -1(不基于indexOf/includes方法)

    /** 1.循环原始字符串的每一项,让每一项从当前位置向后截取 H.length 个字符, 然后和 Y 进行比较,如果不一样,继续循环:如果一样返回当前索引即可 **/ function myInde ...

  3. 实现 (5).add(3).minus(2),使其输出结果为:6

    function check(n) { n = Number(n); return isNaN(n) ? 0 : n; } function add(n) { n = check(n); return ...

  4. (Java实现)洛谷 P2095 营养膳食

    题目描述 Mr.L正在完成自己的增肥计划. 为了增肥,Mr.L希望吃到更多的脂肪.然而也不能只吃高脂肪食品,那样的话就会导致缺少其他营养.Mr.L通过研究发现:真正的营养膳食规定某类食品不宜一次性吃超 ...

  5. Java实现 LeetCode 430 扁平化多级双向链表

    430. 扁平化多级双向链表 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例 ...

  6. Java实现 LeetCode 416 分割等和子集

    416. 分割等和子集 给定一个只包含正整数的非空数组.是否可以将这个数组分割成两个子集,使得两个子集的元素和相等. 注意: 每个数组中的元素不会超过 100 数组的大小不会超过 200 示例 1: ...

  7. Java实现统计方案

    统计方案 题目描述 在一无限大的二维平面中,我们做如下假设: 1.每次只能移动一格: 2.不能向后走(假设你的目的地是"向上",那么你可以向左走,可以向右走,也可以向上走,但是不可 ...

  8. Java实现 LeetCode 397 整数替换

    397. 整数替换 给定一个正整数 n,你可以做如下操作: 如果 n 是偶数,则用 n / 2替换 n. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n. n 变为 1 所需的最小替换次数 ...

  9. Java实现蓝桥杯VIP算法训练 相邻字母

    试题 算法训练 相邻字母 资源限制 时间限制:1.0s 内存限制:256.0MB [问题描述] 从键盘输入一个英文字母,要求按字母的顺序打印出3个相邻的字母,指定的字母在中间.若指定的字母为Z,则打印 ...

  10. UVIYN MMDVM充电宝支持APRS与 YSF

    需求就是要在APRS地图上显示对讲机位置 1.打开pi-star首页链接配置的专家(EXPERT)设置 下面链接快速打开 http://ip/admin/expert/edit_ysfgateway. ...