最近一直在看线程知识,然后看到Timer定时器使用了线程实现的定时功能,于是了解了解;

本文 从Time类的使用和源码分析两个方面讲解:

1---Timer类使用

2---源码分析

1、Time类使用:

  根据是否循环执行分为两类:

         //只执行一次
public void schedule(TimerTask task, long delay);
public void schedule(TimerTask task, Date time); //循环执行
// 在循环执行类别中根据循环时间间隔又可以分为两类
public void schedule(TimerTask task, long delay, long period) ;
public void schedule(TimerTask task, Date firstTime, long period) ; public void scheduleAtFixedRate(TimerTask task, long delay, long period)
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

示例:

只执行一次:

         Timer timer = new Timer();

         //延迟1000ms执行程序
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("IMP 当前时间" + this.scheduledExecutionTime());
}
}, 1000);
//延迟10000ms执行程序
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("IMP 当前时间" + this.scheduledExecutionTime());
}
}, new Date(System.currentTimeMillis() + 10000));

循环执行:

         Timer timer = new Timer();

         //前一次执行程序结束后 2000ms 后开始执行下一次程序
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("IMP 当前时间" + this.scheduledExecutionTime());
}
}, 0,2000); //前一次程序执行开始 后 2000ms后开始执行下一次程序
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("IMP 当前时间" + this.scheduledExecutionTime());
}
},0,2000);

2、源码分析:

Timer 源码:

程序运行:

在初始化Timer时 ,开启一个线程循环提取任务数组中的任务,如果任务数组为空,线程等待直到添加任务;

当添加任务时,唤醒线程,提取数组中标记为1的任务,

如果该任务状态为CANCELLED,则从数组中删除任务,continue ,继续循环提取任务;

然后将当前时间与任务执行时间点比较 标记taskFired=executionTime<=currentTime;

taskFired =false ,说明任务执行时间还没到,则调用wait等待(executionTime-currentTime) 时间长度,然后循环重新提取该任务;

taskFired =true,说明任务执行时间已经到了,或者过去了。继续判断 任务循环时间间隔period;

period=0时,说明此次任务是非循环任务,直接将该任务从数组中删除,并将状态置为EXECUTED,然后执行任务的run方法!

period!=0时,说明此次任务时循环任务,将该任务的执行时间点向前推进,具体推进时间根据调用的方法判断;

如果是schedule方法,则在当前时间基础上向前推进period时间长度;

如果是scheduleAtFixedRate方法,则在当前任务执行时间点基础上向前推进period时间长度,

最后执行任务的run方法;循环提取任务

 package java.util;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger; public class Timer { private final TaskQueue queue = new TaskQueue(); private final TimerThread thread = new TimerThread(queue); private final Object threadReaper = new Object() {
protected void finalize() throws Throwable {
synchronized(queue) {
thread.newTasksMayBeScheduled = false;
queue.notify(); // In case queue is empty.
}
}
}; private final static AtomicInteger nextSerialNumber = new AtomicInteger(0);
private static int serialNumber() {
return nextSerialNumber.getAndIncrement();
} public Timer() {
this("Timer-" + serialNumber());
} public Timer(boolean isDaemon) {
this("Timer-" + serialNumber(), isDaemon);
} public Timer(String name) {
thread.setName(name);
thread.start();
} //在初始化Timer时,确定线程名称,以及是否是守护线程 ,开启线程
public Timer(String name, boolean isDaemon) {
thread.setName(name);
thread.setDaemon(isDaemon);
thread.start();
} 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, Date time) {
sched(task, time.getTime(), 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 schedule(TimerTask task, Date firstTime, long period) {
if (period <= 0)
throw new IllegalArgumentException("Non-positive period.");
sched(task, firstTime.getTime(), -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);
} public void scheduleAtFixedRate(TimerTask task, Date firstTime,
long period) {
if (period <= 0)
throw new IllegalArgumentException("Non-positive period.");
sched(task, firstTime.getTime(), period);
} 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;
} queue.add(task);
if (queue.getMin() == task)
queue.notify();
}
} public void cancel() {
synchronized(queue) {
thread.newTasksMayBeScheduled = false;
queue.clear();
queue.notify(); // In case queue was already empty.
}
} //净化,清除timer中标记为CANCELLED的TIMETASK,  返回值为清除个数
public int purge() {
int result = 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;
}
} //自定义线程
class TimerThread extends Thread { boolean newTasksMayBeScheduled = true; private TaskQueue queue; TimerThread(TaskQueue queue) {
this.queue = queue;
} public void run() {
try {
mainLoop();
} finally {
// Someone killed this Thread, behave as if Timer cancelled
synchronized(queue) {
newTasksMayBeScheduled = false;
queue.clear(); // Eliminate obsolete references
}
}
} 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;
}
currentTime = System.currentTimeMillis();
executionTime = task.nextExecutionTime;
if (taskFired = (executionTime<=currentTime)) {
if (task.period == 0) { // 循环条件为0时,直接清除任务,并将任务状态置为非循环任务,并执行一次任务!
queue.removeMin();
task.state = TimerTask.EXECUTED;
} else { //区分 两种循环类别的关键
queue.rescheduleMin(
task.period<0 ? currentTime - task.period
: executionTime + task.period);
}
}
}
if (!taskFired) // 当下次执行任务时间大于当前时间 等待
queue.wait(executionTime - currentTime);
}
if (taskFired) // 执行任务
task.run();
} catch(InterruptedException e) {
}
}
}
} /**
*
* 任务管理内部类
*/
class TaskQueue { //初始化 128个空间,实际使用127个 位置编号为0的位置不使用
private TimerTask[] queue = new TimerTask[128]; private int size = 0; int size() {
return size;
} //添加任务,如果空间不足,空间*2,,然后排序(将nextExecutionTime最小的排到1位置)
void add(TimerTask task) {
// Grow backing store if necessary
if (size + 1 == queue.length)
queue = Arrays.copyOf(queue, 2*queue.length); queue[++size] = task;
fixUp(size);
} //得到最小的nextExecutionTime的任务
TimerTask getMin() {
return queue[1];
} //得到指定位置的任务
TimerTask get(int i) {
return queue[i];
} //删除最小nextExecutionTime的任务,排序(将nextExecutionTime最小的排到1位置)
void removeMin() {
queue[1] = queue[size];
queue[size--] = null; // Drop extra reference to prevent memory leak
fixDown(1);
} //快速删除指定位置的任务
void quickRemove(int i) {
assert i <= size; queue[i] = queue[size];
queue[size--] = null; // Drop extra ref to prevent memory leak
} //重新设置最小nextExecutionTime的任务的nextExecutionTime,排序(将nextExecutionTime最小的排到1位置)
void rescheduleMin(long newTime) {
queue[1].nextExecutionTime = newTime;
fixDown(1);
} //数组是否为空
boolean isEmpty() {
return size==0;
} //清空数组
void clear() {
// Null out task references to prevent memory leak
for (int i=1; i<=size; i++)
queue[i] = null; size = 0;
} //将nextExecutionTime最小的排到1位置
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;
}
} //将nextExecutionTime最小的排到1位置
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;
}
} //排序(将nextExecutionTime最小的排到1位置) 在快速删除任务后调用
void heapify() {
for (int i = size/2; i >= 1; i--)
fixDown(i);
}
}

TimerTask源码:

功能:用户任务,Timer执行任务实体(任务状态,任务下次执行时间点,任务循环时间间隔,任务本体【run】)

 package java.util;
/**
* 虽然实现了Runnable接口 但是在Timer中直接调用run方法,
* */
public abstract class TimerTask implements Runnable { final Object lock = new Object(); 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;// 循环时间间隔 protected TimerTask() {
} public abstract void run();//自定义任务 //退出 任务执行完毕后,退出返回 true ,未执行完 就退出 返回false
public boolean cancel() {
synchronized(lock) {
boolean result = (state == SCHEDULED);
state = CANCELLED;
return result;
}
}
//返回 时间
public long scheduledExecutionTime() {
synchronized(lock) {
return (period < 0 ? nextExecutionTime + period
: nextExecutionTime - period);
}
}
}

java之 Timer 类的使用以及深入理解的更多相关文章

  1. java之 Timer 类的简单使用案例

              (如果您看到本文章务必看结尾!) 第一次用Timer类,记录一下个人理解. 场景:做苹果内容结果验证时,根据苹果支付凭证去苹果官方服务器验证是否支付成功.但因为苹果服务器比较慢,第 ...

  2. 两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz

    1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 import java.util.TimerTask; public class EmailReportT ...

  3. java中Timer类的详细介绍(详解)

    一.概念 定时计划任务功能在Java中主要使用的就是Timer对象,它在内部使用多线程的方式进行处理,所以它和多线程技术还是有非常大的关联的.在JDK中Timer类主要负责计划任务的功能,也就是在指定 ...

  4. [ImportNew]Java中的Timer类和TimerTask类

    http://www.importnew.com/9978.html java.util.Timer是一个实用工具类,该类用来调度一个线程,使它可以在将来某一时刻执行. Java的Timer类可以调度 ...

  5. java的Timer和TimerTask

    java中Timer类使用的方法是如下的: Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() ...

  6. Java定时器Timer,TimerTask每隔一段时间随机生成数字

    1:java.util.Timer类是一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行. 2:TimerTask类是由 Timer 安排为一次执行或重复执行的任务 ...

  7. java Timer类

    java.util 类 Timer java.lang.Object java.util.Timer public class Timerextends Object 一种工具,线程用其安排以后在后台 ...

  8. Java:利用java Timer类实现定时执行任务的功能

    一.概述 在java中实现定时执行任务的功能,主要用到两个类,Timer和TimerTask类.其中Timer是用来在一个后台线程按指定的计划来执行指定的任务.TimerTask一个抽象类,它的子类代 ...

  9. 【外文翻译】使用Timer类去调度任务 ——java

    使用Timer类去调度任务 --java 原文地址:https://dzone.com/articles/using-timer-class-to-schedule-tasks 原文作者:Jay Sr ...

随机推荐

  1. 跨站的艺术-XSS Fuzzing 的技巧

    作者 | 张祖优(Fooying)  腾讯云 云鼎实验室 对于XSS的漏洞挖掘过程,其实就是一个使用Payload不断测试和调整再测试的过程,这个过程我们把它叫做Fuzzing:同样是Fuzzing, ...

  2. 在 WPF 中使用 Path 路径

    在 WPF 中总会修改 Button 的 Style,比如一个自定义的 Close 按钮.刚入门的可能会用一张 PNG 格式的图片来做这个按钮的 Icon,但这个是不优雅的.而且你要改的时候还得去操作 ...

  3. CentOS 7 安装Broadcom无线网卡驱动

    重新坑了小伙伴一台电脑,用来装centOS练习和做服务器用,哈哈哈 装了了CentOS 7后发现无线网卡读不出来,没有装驱动,网卡是Broadcom ╮(╯_╰)╭ 1.首先确定网卡是否为坑爹类型Br ...

  4. Verilog读写文件

    在通过编写Verilog代码实现ram功能时,需要自己先计算寄存器的位数和深度再编写代码. 而如果需要在编写的ram中预置值的话,就需要使用Verilog语言编写程序读写文件,来将相应的数据赋给寄存器 ...

  5. 老李分享:https协议

    老李分享:https协议   最近我们看到很多站点使用 HTTPS 协议提供网页服务.通常情况下我们都是在一些包含机密信息的站点像银行看到 HTTPS 协议. 如果你访问 google,查看一下地址栏 ...

  6. 手机自动化测试:Appium源码分析之跟踪代码分析五

    手机自动化测试:Appium源码分析之跟踪代码分析五   手机自动化测试是未来很重要的测试技术,作为一名测试人员应该熟练掌握,POPTEST举行手机自动化测试的课程,希望可以训练出优秀的手机测试开发工 ...

  7. Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)

    题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...

  8. 提交到SVN中的项目被删除 且项目名已经被新建项目占用找回方法

    提到项目找回,一看就头疼,找回起来较麻烦.下面就讲一下. 首先,确定项目是否被删除?找项目,太多了,都被找一遍了,还是没找到,看看就头痛,换了个方法,找了个项目的包,xx.apk,反编译下吧,过程略, ...

  9. html中的Flash对象

    开源Flash播放器 http://www.open-open.com/ajax/Video.htm

  10. Unity编译Android的原理解析和apk打包分析

    作者介绍:张坤 最近由于想在Scene的脚本组件中,调用Android的Activity的相关接口,就需要弄明白Scene和Activity的实际对应关系,并对Unity调用Android的部分原理进 ...