Java 定时任务调度 (1) 定时任务原理与实践
这是我参与新手入门的第1篇文章。
1.0 简单的例子
public static void main(String[] args) {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("TimerTask当前时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
};
Timer timer = new Timer();
long delay = 0;
long period = 1000;
timer.schedule(timerTask, delay, period);
}
效果:
TimerTask的原理是什么那?
实际上通过上方的示例代码你就能发现,TimerTask就是一个实现了run方法的类,TimerTask是一个抽象类,实现了Runnable,提供了抽象方法run()。
public abstract class TimerTask implements Runnable {
//task的状态,默认是VIRGIN,共有4个状态
int state = VIRGIN;
//未执行
static final int VIRGIN = 0;
//非重复任务,未被执行
static final int SCHEDULED = 1;
//非重复任务已执行或正执行,并且没有被取消
static final int EXECUTED = 2;
//已经取消
static final int CANCELLED = 3;
//下次执行时间
long nextExecutionTime;
//任务执行的周期
long period = 0;
/**
* The action to be performed by this timer task.
*/
public abstract void run();
//这个就是把当前这个task设置为取消状态,Timer也有一个cancel方法,会面会提到
public boolean cancel() {
synchronized(lock) {
boolean result = (state == SCHEDULED);
state = CANCELLED;
return result;
}
}
//还有一个scheduledExecutionTime方法 就不提了
}
关键在于 Timer
????
1.1 Timer的构造函数
- 先来看一下第三个构造方法,方法体就是传入并设置线程的名字,然后启动。
public Timer(String name) {
thread.setName(name);
thread.start();
}
这里的thread就是一个线程,
/**
* The timer thread.
*/
private final TimerThread thread = new TimerThread(queue);
而这个TimerThread就被定义在Timer类中,从注释中可以看出,这个线程就是用来执行定时任务的具体线程
,当队列中的任务被触发时执行它们。
我会在1.3节中讲这个TimerThread
和队列TaskQueue
。????
/**
* This "helper class" implements the timer's task execution thread, which
* waits for tasks on the timer queue, executions them when they fire,
* reschedules repeating tasks, and removes cancelled tasks and spent
* non-repeating tasks from the queue.
*/
class TimerThread extends Thread {
}
此时,回过头来再看其他构造函数就一目了然了,第一个无参构造函数通过Timer为前缀名构造一个线程,里面的this就是上面这个构造函数,这里的serialNumber()
就是去生成一个名字。
public Timer() {
this("Timer-" + serialNumber());
}
/**
* This ID is used to generate thread names.
*/
private final static AtomicInteger nextSerialNumber = new AtomicInteger(0);
private static int serialNumber() {
return nextSerialNumber.getAndIncrement();
}
如果有不明白AtomicInteger()
的朋友可以看着这篇博客: blog.****.net/fanrenxiang…
在代码里打个断点就可以清楚的看到这个线程的名字:
-
第二个构造函数传入了是否为后台线程,如果是,主线程结束后会自动结束,不需要调用cancel。
public Timer(boolean isDaemon) {
this("Timer-" + serialNumber(), isDaemon);
}
为什么要设置为后台线程
,很经典的一个例子是在web应用中使用线程,当你把web应用关闭后,这个线程还在运行!????????
这是因为线程是JVM级别的,web应用关闭后,这个线程并没有销毁。具体可以看这篇博客:blog.****.net/chetianyao8…
-
第四个构造函数可以设置名字及是否为后台线程,并且启动。
public Timer(String name, boolean isDaemon) {
thread.setName(name);
thread.setDaemon(isDaemon);
thread.start();
}
好,下面开始讲Timer的方法,属性放到1.3节中
1.2 Timer的方法
timer提供了6个调度方法,其实都大同小异,先了解一下,看完1.3节再回过来看
public void schedule(TimerTask task, long delay) {
if (delay < 0)
throw new IllegalArgumentException("Negative delay.");
sched(task, System.currentTimeMillis()+delay, 0);
}
public void schedule(TimerTask task, long delay, long period) {
if (delay < 0)
throw new IllegalArgumentException("Negative delay.");
if (period <= 0)
throw new IllegalArgumentException("Non-positive period.");
sched(task, System.currentTimeMillis()+delay, -period);
}
public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
if (delay < 0)
throw new IllegalArgumentException("Negative delay.");
if (period <= 0)
throw new IllegalArgumentException("Non-positive period.");
sched(task, System.currentTimeMillis()+delay, period);
}
可以发现,最终都调用了sched
方法,而schedule
和scheduleAtFixedRate
的区别就是传入的最终period的正负数,为什么会有这种区别?看完后面就懂了。
sched方法就是对task做一些设置,加到队列里,并做一次notify操作
。这些后面都会提到。
private void sched(TimerTask task, long time, long period) {
if (time < 0)
throw new IllegalArgumentException("Illegal execution time.");
// Constrain value of period sufficiently to prevent numeric
// overflow while still being effectively infinitely large.
if (Math.abs(period) > (Long.MAX_VALUE >> 1))
period >>= 1;
synchronized(queue) {
if (!thread.newTasksMayBeScheduled)
throw new IllegalStateException("Timer already cancelled.");
synchronized(task.lock) {
if (task.state != TimerTask.VIRGIN)
throw new IllegalStateException(
"Task already scheduled or cancelled");
task.nextExecutionTime = time; //下次执行时间
task.period = period; //周期
task.state = TimerTask.SCHEDULED; //task状态
}
//添加到队列
queue.add(task);
if (queue.getMin() == task)
queue.notify();
}
}
cancel()
方法,一旦执行,Timer就停掉了,为什么要把newTasksMayBeScheduled设置为false
以及为什调用notify(),也需要看后面的讲解。
public void cancel() {
synchronized(queue) {
thread.newTasksMayBeScheduled = false;
queue.clear();
queue.notify(); // In case queue was already empty.
}
}
purge()
方法,当对TimerTask做了多次cancel之后,队列就混乱了,这时候就需要调用这个方法,回收空间并重新排列。
注意,这个cancel不是上面讲Timer的cancel方法,是TimerTask的cancel方法
。
public int purge() {
int re0sult = 0;
synchronized(queue) {
for (int i = queue.size(); i > 0; i--) {
if (queue.get(i).state == TimerTask.CANCELLED) {
queue.quickRemove(i);
result++;
}
}
if (result != 0)
queue.heapify();
}
return result;
}
1.3 Timer的属性
在上面已经提到过了Timer的两个属性thread和nextSerialNumber,此外还有一个queue,这个queue存储的就是要调度的任务,也就是TimerTask。
1.3.1 TaskQueue
TaskQueue的结构很简单,一个数组,一个size。需要注意的一点是此队列存储范围是queue[1]-queue[size]
。队列默认大小是128,但,是支持扩容的。
private final TaskQueue queue = new TaskQueue();
class TaskQueue {
private TimerTask[] queue = new TimerTask[128];
private int size = 0;
}
扩容的方式是在增加TimerTask时进行操作
void add(TimerTask task) {
// 扩容在这
if (size + 1 == queue.length)
queue = Arrays.copyOf(queue, 2*queue.length);
queue[++size] = task;
fixUp(size);
}
TaskQueue提供了一系列的操作来对队列进行处理,尤其是排序。这里面涉及不少数据结构的操作,大学数据结构不及格的可以学一波。
TaskQueue原理上是一个平衡二叉堆
根节点的nextExecutionTime最小,queue[n]的子节点是queue[2n]和queue[2n+1],这些乱七八糟的就不说了,课本上都有。
size()、get(i)和add(TimerTask)这三个方法就不说了,getMin()是返回最近需要执行的任务,返回的就是queue[1]。
removeMin()
是删除当前最近执行的任务,而删除的操作很经典,大学数据结构必学的点,就是把队尾赋给对头,队尾置为null,并且重新排序。
void removeMin() {
queue[1] = queue[size];
queue[size--] = null; // Drop extra reference to prevent memory leak
fixDown(1);
}
fixDown(int)
的作用就是下滤,不断的把queue[k]和它的子节点进行比较,直到它的nextExecutionTime小于等于子节点。
private void fixDown(int k) {
int j;
while ((j = k << 1) <= size && j > 0) {
if (j < size &&
queue[j].nextExecutionTime > queue[j+1].nextExecutionTime)
j++; // j indexes smallest kid
if (queue[k].nextExecutionTime <= queue[j].nextExecutionTime)
break;
TimerTask tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
k = j;
}
}
而fixUp(int)
的作用是上溢,不断向上提升,在新增TimerTask时使用。
private void fixUp(int k) {
while (k > 1) {
int j = k >> 1;
if (queue[j].nextExecutionTime <= queue[k].nextExecutionTime)
break;
TimerTask tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
k = j;
}
}
quickRemove(int)
删除指定的元素。为什么叫quick那,它只赋值没有排序,排序要搭配heapify()。
void quickRemove(int i) {
assert i <= size;
queue[i] = queue[size];
queue[size--] = null; // Drop extra ref to prevent memory leak
}
void heapify() {
//对queue的后半段做排序
for (int i = size/2; i >= 1; i--)
fixDown(i);
}
rescheduleMin(newTime)
重新设置当前任务的下一次执行时间并排序,为什么取queue[1]?
因为在真正执行的时候,是每次取下次执行时间最短的,也就是queue[1]。
void rescheduleMin(long newTime) {
queue[1].nextExecutionTime = newTime;
fixDown(1);
}
1.3.2 TimerThread
在1.1节简单的提了下TimerThread,这里重点介绍下,因为它是Timer的重点????????????
TimerThread很简单,TimerThread就是具体去执行的地方。
先来看下TimerThread的源码,主体就是 mainLoop()
方法。
class TimerThread extends Thread {
//标志位,用于在mainLoop中判断状态
boolean newTasksMayBeScheduled = true;
private TaskQueue queue;
TimerThread(TaskQueue queue) {
this.queue = queue;
}
public void run() {
try {
//mainLoop的具体实现,下面会讲
mainLoop();
} finally {
// 将参数置为false,并且队列清空
synchronized(queue) {
newTasksMayBeScheduled = false;
queue.clear(); // Eliminate obsolete references
}
}
}
}
那么TimerThread是如何运行的?我来给你捋一下
//一开始我们是new了一个Timer(),通过Timer的无参构造函数配置了默认的线程名字并执行了thread.start()
//并且通过Timer的schedule()方法把TimerTask放到队列里并设置了延迟、周期和状态。
Timer timer = new Timer();
timer.schedule(timerTask, delay, period);
public Timer() {
this("Timer-" + serialNumber());
}
public Timer(String name) {
//这里的thread就是被final修饰的TimerThread,new了一个TimerThread并传递了队列queue
//private final TimerThread thread = new TimerThread(queue);
thread.setName(name);
thread.start();
}
//而TimerThread是有一个TaskQueue的属性和重载的构造函数,这个重载的构造函数接收了queue
private TaskQueue queue;
TimerThread(TaskQueue queue) {
this.queue = queue;
}
//一旦执行thread.start(),TimerThread的run方法就会执行,具体执行就在mainLoop()方法中
private void mainLoop() {
while (true) {
try {
TimerTask task;
boolean taskFired;
synchronized(queue) {
// Wait for queue to become non-empty
while (queue.isEmpty() && newTasksMayBeScheduled)
queue.wait();
if (queue.isEmpty())
break; // Queue is empty and will forever remain; die
// Queue nonempty; look at first evt and do the right thing
long currentTime, executionTime;
task = queue.getMin();
synchronized(task.lock) {
if (task.state == TimerTask.CANCELLED) {
queue.removeMin();
continue; // No action required, poll queue again
}
currentTime = System.currentTimeMillis();
executionTime = task.nextExecutionTime;
if (taskFired = (executionTime<=currentTime)) {
if (task.period == 0) { // Non-repeating, remove
queue.removeMin();
task.state = TimerTask.EXECUTED;
} else { // Repeating task, reschedule
queue.rescheduleMin(
task.period<0 ? currentTime - task.period
: executionTime + task.period);
}
}
}
if (!taskFired) // Task hasn't yet fired; wait
queue.wait(executionTime - currentTime);
}
if (taskFired) // Task fired; run it, holding no locks
task.run();
} catch(InterruptedException e) {
}
}
}
//大体读一下这个方法,你就会发现除非遇到break或者遇到不能捕获的异常,它就是个死循环。
while (queue.isEmpty() && newTasksMayBeScheduled)
queue.wait();
//跳出循环的条件就是queue不为空或者newTasksMayBeScheduled为false
//那么wait就是等到其他地方对queue发生nitify操作,其实就是在调用cancel的时候
//这时标志位是false,会跳出循环,并且对queue设置了clear操作, 直接就跳出了外部的死循环。
public void cancel() {
synchronized(queue) {
thread.newTasksMayBeScheduled = false;
queue.clear();
queue.notify(); // In case queue was already empty.
}
}
//还有一处,是Timer的属性threadReaper调用finalize的时候
//这个threadReaper只重写了finalize方法,GC的时候调用
private final Object threadReaper = new Object() {
protected void finalize() throws Throwable {
synchronized(queue) {
thread.newTasksMayBeScheduled = false;
queue.notify(); // In case queue is empty.
}
}
};
//还有就是当对queue执行add操作的时候,在Timer中的sched方法,前边有提到。此时queue不为空,就跳出了循环。
//之后是判断该task是否被取消
if (task.state == TimerTask.CANCELLED) {
queue.removeMin();
continue; // No action required, poll queue again
}
//再之后是取当前的系统时间和上次预计的执行时间,如果当前系统时间已经超了,就赶紧执行。
//不过在执行之前需要判断是否是重复任务。
//判断一下period是否为0,0就代表一次性任务,删掉。如果不是,就调用rescheduleMin设置下一次执行时间并排序。
currentTime = System.currentTimeMillis();
executionTime = task.nextExecutionTime;
if (taskFired = (executionTime<=currentTime)) {
if (task.period == 0) { // Non-repeating, remove
queue.removeMin();
task.state = TimerTask.EXECUTED;
} else { // Repeating task, reschedule
queue.rescheduleMin(
task.period<0 ? currentTime - task.period
: executionTime + task.period);
}
}
//这里很有意思,如果period是负数,下次执行时间就是当前系统时间+周期时间。如果是正数就是原计算的下次执行时间+周期时间。
//这就是schedule和scheduleAtFixedRate的区别。换一下参数的正负数,就和另一个方法一样。。
//再往后就是如果当前这个task执行时间还没到就等待一段时间,
if (!taskFired) // Task hasn't yet fired; wait
queue.wait(executionTime - currentTime);
//如果时间到了,就执行了
if (taskFired) // Task fired; run it, holding no locks
task.run();
好,至此整个执行流程就结束了,是不是很简单!????????????
在回过头去看1.2节,是不是清晰又明了!????
1.4 Timer总结
Timer可以分四个部分:
- TimerTask是调度任务的具体内容
- TaskQueue存放要执行的TimerTask,下标越小优先级最高。
- TimerThread是Thread的扩展类,从TaskQueue中获取下标为1 的TimerTask执行,并根据是否是重复任务对TaskQueue进行处理。
- Timer主要就是配置任务执行时间、间隔、执行内容,TimerThread和TaskQueue位于Timer类中。
需要注意的点是,如果要用TimerTask,一定要记得使用try catch,如果遇到不能捕获的异常Timer就终止了。
这里可以参考一个生产过程中遇到的问题:blog.verysu.com/article/435
推荐阅读
-
Java 定时任务调度 (1) 定时任务原理与实践
-
windows下进程间通信的(13种方法)-摘 要 本文讨论了进程间通信与应用程序间通信的含义及相应的实现技术,并对这些技术的原理、特性等进行了深入的分析和比较。 ---- 关键词 信号 管道 消息队列 共享存储段 信号灯 远程过程调用 Socket套接字 MQSeries 1 引言 ---- 进程间通信的主要目的是实现同一计算机系统内部的相互协作的进程之间的数据共享与信息交换,由于这些进程处于同一软件和硬件环境下,利用操作系统提供的的编程接口,用户可以方便地在程序中实现这种通信;应用程序间通信的主要目的是实现不同计算机系统中的相互协作的应用程序之间的数据共享与信息交换,由于应用程序分别运行在不同计算机系统中,它们之间要通过网络之间的协议才能实现数据共享与信息交换。进程间通信和应用程序间通信及相应的实现技术有许多相同之处,也各有自己的特色。即使是同一类型的通信也有多种的实现方法,以适应不同情况的需要。 ---- 为了充分认识和掌握这两种通信及相应的实现技术,本文将就以下几个方面对这两种通信进行深入的讨论:问题的由来、解决问题的策略和方法、每种方法的工作原理和实现、每种实现方法的特点和适用的范围等。 2 进程间的通信及其实现技术 ---- 用户提交给计算机的任务最终都是通过一个个的进程来完成的。在一组并发进程中的任何两个进程之间,如果都不存在公共变量,则称该组进程为不相交的。在不相交的进程组中,每个进程都独立于其它进程,它的运行环境与顺序程序一样,而且它的运行环境也不为别的进程所改变。运行的结果是确定的,不会发生与时间相关的错误。 ---- 但是,在实际中,并发进程的各个进程之间并不是完全互相独立的,它们之间往往存在着相互制约的关系。进程之间的相互制约关系表现为两种方式: ---- (1) 间接相互制约:共享CPU ---- (2) 直接相互制约:竞争和协作 ---- 竞争——进程对共享资源的竞争。为保证进程互斥地访问共享资源,各进程必须互斥地进入各自的临界段。 ---- 协作——进程之间交换数据。为完成一个共同任务而同时运行的一组进程称为同组进程,它们之间必须交换数据,以达到协作完成任务的目的,交换数据可以通知对方可以做某事或者委托对方做某事。 ---- 共享CPU问题由操作系统的进程调度来实现,进程间的竞争和协作由进程间的通信来完成。进程间的通信一般由操作系统提供编程接口,由程序员在程序中实现。UNIX在这个方面可以说最具特色,它提供了一整套进程间的数据共享与信息交换的处理方法——进程通信机制(IPC)。因此,我们就以UNIX为例来分析进程间通信的各种实现技术。 ---- 在UNIX中,文件(File)、信号(Signal)、无名管道(Unnamed Pipes)、有名管道(FIFOs)是传统IPC功能;新的IPC功能包括消息队列(Message queues)、共享存储段(Shared memory segment)和信号灯(Semapores)。 ---- (1) 信号 ---- 信号机制是UNIX为进程中断处理而设置的。它只是一组预定义的值,因此不能用于信息交换,仅用于进程中断控制。例如在发生浮点错、非法内存访问、执行无效指令、某些按键(如ctrl-c、del等)等都会产生一个信号,操作系统就会调用有关的系统调用或用户定义的处理过程来处理。 ---- 信号处理的系统调用是signal,调用形式是: ---- signal(signalno,action) ---- 其中,signalno是规定信号编号的值,action指明当特定的信号发生时所执行的动作。 ---- (2) 无名管道和有名管道 ---- 无名管道实际上是内存中的一个临时存储区,它由系统安全控制,并且独立于创建它的进程的内存区。管道对数据采用先进先出方式管理,并严格按顺序操作,例如不能对管道进行搜索,管道中的信息只能读一次。 ---- 无名管道只能用于两个相互协作的进程之间的通信,并且访问无名管道的进程必须有共同的祖先。 ---- 系统提供了许多标准管道库函数,如: pipe——打开一个可以读写的管道; close——关闭相应的管道; read——从管道中读取字符; write——向管道中写入字符; ---- 有名管道的操作和无名管道类似,不同的地方在于使用有名管道的进程不需要具有共同的祖先,其它进程,只要知道该管道的名字,就可以访问它。管道非常适合进程之间快速交换信息。 ---- (3) 消息队列(MQ) ---- 消息队列是内存中独立于生成它的进程的一段存储区,一旦创建消息队列,任何进程,只要具有正确的的访问权限,都可以访问消息队列,消息队列非常适合于在进程间交换短信息。 ---- 消息队列的每条消息由类型编号来分类,这样接收进程可以选择读取特定的消息类型——这一点与管道不同。消息队列在创建后将一直存在,直到使用msgctl系统调用或iqcrm -q命令删除它为止。 ---- 系统提供了许多有关创建、使用和管理消息队列的系统调用,如: ---- int msgget(key,flag)——创建一个具有flag权限的MQ及其相应的结构,并返回一个唯一的正整数msqid(MQ的标识符); ---- int msgsnd(msqid,msgp,msgsz,msgtyp,flag)——向队列中发送信息; ---- int msgrcv(msqid,cmd,buf)——从队列中接收信息; ---- int msgctl(msqid,cmd,buf)——对MQ的控制操作; ---- (4) 共享存储段(SM) ---- 共享存储段是主存的一部分,它由一个或多个独立的进程共享。各进程的数据段与共享存储段相关联,对每个进程来说,共享存储段有不同的虚拟地址。系统提供的有关SM的系统调用有: ---- int shmget(key,size,flag)——创建大小为size的SM段,其相应的数据结构名为key,并返回共享内存区的标识符shmid; ---- char shmat(shmid,address,flag)——将当前进程数据段的地址赋给shmget所返回的名为shmid的SM段; ---- int shmdr(address)——从进程地址空间删除SM段; ---- int shmctl (shmid,cmd,buf)——对SM的控制操作; ---- SM的大小只受主存限制,SM段的访问及进程间的信息交换可以通过同步读写来完成。同步通常由信号灯来实现。SM非常适合进程之间大量数据的共享。 ---- (5) 信号灯 ---- 在UNIX中,信号灯是一组进程共享的数据结构,当几个进程竞争同一资源时(文件、共享内存或消息队列等),它们的操作便由信号灯来同步,以防止互相干扰。 ---- 信号灯保证了某一时刻只有一个进程访问某一临界资源,所有请求该资源的其它进程都将被挂起,一旦该资源得到释放,系统才允许其它进程访问该资源。信号灯通常配对使用,以便实现资源的加锁和解锁。 ---- 进程间通信的实现技术的特点是:操作系统提供实现机制和编程接口,由用户在程序中实现,保证进程间可以进行快速的信息交换和大量数据的共享。但是,上述方式主要适合在同一台计算机系统内部的进程之间的通信。 3 应用程序间的通信及其实现技术 ---- 同进程之间的相互制约一样,不同的应用程序之间也存在竞争和协作的关系。UNIX操作系统也提供一些可用于应用程序之间实现数据共享与信息交换的编程接口,程序员可以通过自己编程来实现。如远程过程调用和基于TCP/IP协议的套接字(Socket)编程。但是,相对普通程序员来说,它们涉及的技术比较深,编程也比较复杂,实现起来困难较大。 ---- 于是,一种新的技术应运而生——通过将有关通信的细节完全掩盖在某个独立软件内部,即底层的通讯工作和相应的维护管理工作由该软件内部来实现,用户只需要将通信任务提交给该软件去完成,而不必理会它的具体工作过程——这就是所谓的中间件技术。 ---- 我们在这里分别讨论这三种常用的应用程序间通信的实现技术——远程过程调用、会话编程技术和MQSeries消息队列技术。其中远程过程调用和会话编程属于比较低级的方式,程序员参与的程度较深,而MQSeries消息队列则属于比较高级的方式,即中间件方式,程序员参与的程度较浅。 ---- 4.1 远程过程调用(RPC)