文档章节

Java定时器_Timer

秋风醉了
 秋风醉了
发布于 2015/08/13 23:26
字数 2255
阅读 188
收藏 4
点赞 1
评论 0

Java定时器_Timer

1. Timer和TimerTask

  • Timer是jdk中提供的一个定时器工具,使用的时候会在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次。

  • TimerTask是一个实现了Runnable接口的抽象类,代表一个可以被Timer执行的任务。

 

2. 一个Timer调度的例子

实现Task

package com.usoft.timer;

import java.util.TimerTask;

public class Task extends TimerTask {
    public void run() {
        System.out.println("定时任务执行,taskId=" + this.hashCode());
    }
}

测试Timer

package com.usoft.timer;

import java.util.Calendar;
import java.util.Timer;

public class TimeTaskTest {
    public static void main(String[] args) throws InterruptedException {
        Timer timer = new Timer();
        timer.schedule(new Task(), 10 * 1000); //10 秒延迟执行
        timer.schedule(new Task(), 60 * 1000, 10 * 1000);//60 秒延迟执行,每隔十秒执行一次
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MILLISECOND, 1000 * 60);
        timer.schedule(new Task(), c.getTime()); //当前时间的60秒后执行
        timer.schedule(new Task(), c.getTime(), 10 * 1000);
        timer.scheduleAtFixedRate(new Task(), c.getTime(), 10 * 1000);
        timer.scheduleAtFixedRate(new Task(), 60 * 1000, 10 * 1000);

        Thread.sleep(1000 * 120);
        timer.cancel();//定时器停止运行
    }
}

 

3. 如何终止Timer线程

默认情况下,创建的timer线程会一直执行,主要有下面四种方式来终止timer线程:

  • 调用timer的cancle方法

  • 把timer线程设置成daemon线程,(new Timer(true)创建daemon线程),在jvm里,如果所有用户线程结束,那么守护线程也会被终止,不过这种方法一般不用。

  • 当所有任务执行结束后,删除对应timer对象的引用,线程也会被终止。

  • 调用System.exit方法终止程序

 

4. 关于cancle方式终止线程

这种方式终止timer线程,jdk的实现比较巧妙,稍微说一下。

首先看cancle方法的源码:

public void cancel() {
    synchronized(queue) {
        thread.newTasksMayBeScheduled = false;
        queue.clear();
        queue.notify();  // In case queue was already empty.
    }
}

没有显式的线程stop方法,而是调用了queue的clear方法和queue的notify方法,clear是个自定义方法,notify是Objec自带的方法,很明显是去唤醒wait方法的。

再看clear方法:

void clear() {
    // Null out task references to prevent memory leak
    for (int i=1; i<=size; i++)
        queue[i] = null;

    size = 0;
}

clear方法很简单,就是去清空queue,queue是一个TimerTask的数组,然后把queue的size重置成0,变成empty.还是没有看到显式的停止线程方法,回到最开始new Timer的时候,看看new Timer代码:

public Timer() {
    this("Timer-" + serialNumber());
}

public Timer(String name) {
    thread.setName(name);
    thread.start();
}

看看这个内部变量thread:

/**
 * The timer thread.
 */
private final TimerThread thread = new TimerThread(queue);

不是原生的Thread,是自定义的类TimerThread.这个类实现了Thread类,重写了run方法,如下:

public void run() {
    try {
        mainLoop();
    } finally {
        // Someone killed this Thread, behave as if Timer cancelled
        synchronized(queue) {
            newTasksMayBeScheduled = false;
            queue.clear();  // Eliminate obsolete references
        }
    }
}

最后是这个mainLoop方法,这方法比较长,

/**
 * The main timer loop.  (See class comment.)
 */
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) {
        }
    }
}

可以看到wait方法,之前的notify就是通知到这个wait,然后clear方法在notify之前做了清空数组的操作,所以会break,线程执行结束,退出。

 

5. 反复执行一个任务

通过调用三个参数的schedule方法实现,最后一个参数是执行间隔,单位毫秒。

 

6. schedule VS. scheduleAtFixedRate

/**
 * Schedules the specified task for execution after the specified delay.
 *
 * @param task  task to be scheduled.
 * @param delay delay in milliseconds before task is to be executed.
 */
public void schedule(TimerTask task, long delay) {
	if (delay < 0)
		throw new IllegalArgumentException("Negative delay.");
	sched(task, System.currentTimeMillis()+delay, 0);
}

/**
 * Schedules the specified task for execution at the specified time.  If
 * the time is in the past, the task is scheduled for immediate execution.
 *
 * @param task task to be scheduled.
 * @param time time at which task is to be executed.
 */
public void schedule(TimerTask task, Date time) {
	sched(task, time.getTime(), 0);
}

/**
 * Schedules the specified task for repeated <i>fixed-delay execution</i>,
 * beginning after the specified delay.  Subsequent executions take place
 * at approximately regular intervals separated by the specified period.
 *
 * <p>In fixed-delay execution, each execution is scheduled relative to
 * the actual execution time of the previous execution.  If an execution
 * is delayed for any reason (such as garbage collection or other
 * background activity), subsequent executions will be delayed as well.
 * In the long run, the frequency of execution will generally be slightly
 * lower than the reciprocal of the specified period (assuming the system
 * clock underlying <tt>Object.wait(long)</tt> is accurate).
 *
 * <p>Fixed-delay execution is appropriate for recurring activities
 * that require "smoothness."  In other words, it is appropriate for
 * activities where it is more important to keep the frequency accurate
 * in the short run than in the long run.  This includes most animation
 * tasks, such as blinking a cursor at regular intervals.  It also includes
 * tasks wherein regular activity is performed in response to human
 * input, such as automatically repeating a character as long as a key
 * is held down.
 *
 * @param task   task to be scheduled.
 * @param delay  delay in milliseconds before task is to be executed.
 * @param period time in milliseconds between successive task executions.
 */
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);
}

/**
 * Schedules the specified task for repeated <i>fixed-delay execution</i>,
 * beginning at the specified time. Subsequent executions take place at
 * approximately regular intervals, separated by the specified period.
 *
 * <p>In fixed-delay execution, each execution is scheduled relative to
 * the actual execution time of the previous execution.  If an execution
 * is delayed for any reason (such as garbage collection or other
 * background activity), subsequent executions will be delayed as well.
 * In the long run, the frequency of execution will generally be slightly
 * lower than the reciprocal of the specified period (assuming the system
 * clock underlying <tt>Object.wait(long)</tt> is accurate).  As a
 * consequence of the above, if the scheduled first time is in the past,
 * it is scheduled for immediate execution.
 *
 * <p>Fixed-delay execution is appropriate for recurring activities
 * that require "smoothness."  In other words, it is appropriate for
 * activities where it is more important to keep the frequency accurate
 * in the short run than in the long run.  This includes most animation
 * tasks, such as blinking a cursor at regular intervals.  It also includes
 * tasks wherein regular activity is performed in response to human
 * input, such as automatically repeating a character as long as a key
 * is held down.
 *
 * @param task   task to be scheduled.
 * @param firstTime First time at which task is to be executed.
 * @param period time in milliseconds between successive task executions.
 */
public void schedule(TimerTask task, Date firstTime, long period) {
	if (period <= 0)
		throw new IllegalArgumentException("Non-positive period.");
	sched(task, firstTime.getTime(), -period);
}

/**
 * Schedules the specified task for repeated <i>fixed-rate execution</i>,
 * beginning after the specified delay.  Subsequent executions take place
 * at approximately regular intervals, separated by the specified period.
 *
 * <p>In fixed-rate execution, each execution is scheduled relative to the
 * scheduled execution time of the initial execution.  If an execution is
 * delayed for any reason (such as garbage collection or other background
 * activity), two or more executions will occur in rapid succession to
 * "catch up."  In the long run, the frequency of execution will be
 * exactly the reciprocal of the specified period (assuming the system
 * clock underlying <tt>Object.wait(long)</tt> is accurate).
 *
 * <p>Fixed-rate execution is appropriate for recurring activities that
 * are sensitive to <i>absolute</i> time, such as ringing a chime every
 * hour on the hour, or running scheduled maintenance every day at a
 * particular time.  It is also appropriate for recurring activities
 * where the total time to perform a fixed number of executions is
 * important, such as a countdown timer that ticks once every second for
 * ten seconds.  Finally, fixed-rate execution is appropriate for
 * scheduling multiple repeating timer tasks that must remain synchronized
 * with respect to one another.
 *
 * @param task   task to be scheduled.
 * @param delay  delay in milliseconds before task is to be executed.
 * @param period time in milliseconds between successive task executions.
 * @throws IllegalArgumentException if {@code delay < 0}, or
 *         {@code delay + System.currentTimeMillis() < 0}, or
 *         {@code period <= 0}
 * @throws IllegalStateException if task was already scheduled or
 *         cancelled, timer was cancelled, or timer thread terminated.
 * @throws NullPointerException if {@code task} is null
 */
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);
}

/**
 * Schedules the specified task for repeated <i>fixed-rate execution</i>,
 * beginning at the specified time. Subsequent executions take place at
 * approximately regular intervals, separated by the specified period.
 *
 * <p>In fixed-rate execution, each execution is scheduled relative to the
 * scheduled execution time of the initial execution.  If an execution is
 * delayed for any reason (such as garbage collection or other background
 * activity), two or more executions will occur in rapid succession to
 * "catch up."  In the long run, the frequency of execution will be
 * exactly the reciprocal of the specified period (assuming the system
 * clock underlying <tt>Object.wait(long)</tt> is accurate).  As a
 * consequence of the above, if the scheduled first time is in the past,
 * then any "missed" executions will be scheduled for immediate "catch up"
 * execution.
 *
 * <p>Fixed-rate execution is appropriate for recurring activities that
 * are sensitive to <i>absolute</i> time, such as ringing a chime every
 * hour on the hour, or running scheduled maintenance every day at a
 * particular time.  It is also appropriate for recurring activities
 * where the total time to perform a fixed number of executions is
 * important, such as a countdown timer that ticks once every second for
 * ten seconds.  Finally, fixed-rate execution is appropriate for
 * scheduling multiple repeating timer tasks that must remain synchronized
 * with respect to one another.
 *
 * @param task   task to be scheduled.
 * @param firstTime First time at which task is to be executed.
 * @param period time in milliseconds between successive task executions.
 */
public void scheduleAtFixedRate(TimerTask task, Date firstTime,
								long period) {
	if (period <= 0)
		throw new IllegalArgumentException("Non-positive period.");
	sched(task, firstTime.getTime(), period);
}

这两个方法都是任务调度方法,他们之间区别是,schedule会保证任务的间隔是按照定义的period参数严格执行的,如果某一次调度时间比较长,那么后面的时间会顺延,保证调度间隔都是period,而scheduleAtFixedRate是严格按照调度时间来的,如果某次调度时间太长了,那么会通过缩短间隔的方式保证下一次调度在预定时间执行。举个例子:每隔3秒调度一次,那么正常就是0,3,6,9s这样的时间,如果第二次调度花了2s的时间,如果是schedule,就会变成0,3+2,8,11这样的时间,保证间隔,而scheduleAtFixedRate就会变成0,3+2,6,9,压缩间隔,保证调度时间。

 

7. 一些注意点

每一个Timer仅对应唯一一个线程。Timer不保证任务执行的十分精确。Timer类的线程安全的。

============END============

本文转载自:http://www.cnblogs.com/lingiu/p/3782813.html

共有 人打赏支持
秋风醉了
粉丝 223
博文 581
码字总数 411013
作品 0
东城
程序员
Android中实现定时器的四种方式

Android中实现定时器的四种方式 第一种方式利用Timer和TimerTask 1、继承关系 java.util.Timer 基本方法 schedule 例如: [java] view plaincopyprint? schedule方法有三个参数 第一个参数就...

Yao--靠自己 ⋅ 05/16 ⋅ 0

Java多线程学习(五)线程间通信知识点补充

系列文章传送门: Java多线程学习(一)Java多线程入门 Java多线程学习(二)synchronized关键字(1) java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Ja...

一只蜗牛呀 ⋅ 04/16 ⋅ 0

4个Java的常用工具,了解一下吧!

在现如今的互联网时代里,Java无疑是一种极为流行的开发语言,无论是程序界还是整个互联网行业势必带来很大的影响。不管是人才需求还是薪资水平上,Java的发展前景都是很乐观的。 关于Java的...

梦想远方_8e96 ⋅ 06/15 ⋅ 0

【目录导航】JAVA零基础进阶之路

【JAVA零基础入门系列】(已完结)导航目录 Day1 开发环境搭建 Day2 Java集成开发环境IDEA Day3 Java基本数据类型 Day4 变量与常量 Day5 Java中的运算符 Day6 Java字符串 Day7 Java输入与输出...

MFrank ⋅ 06/21 ⋅ 0

sharding-jdbc源码分析—准备工作

原文作者:阿飞Javaer 原文链接:https://www.jianshu.com/p/7831817c1da8 接下来对sharding-jdbc源码的分析基于tag为源码,根据sharding-jdbc Features深入学习sharding-jdbc的几个主要特性...

飞哥-Javaer ⋅ 05/03 ⋅ 0

培训云计算学校,虚拟机基本结构讲解

我们要对JVM虚拟机的结构有一个感性的认知。毕竟我们不是编程人员,认知程度达不到那么深入。一个运行时的Java虚拟机实例的天职是:负责运行一个java程序。当启动一个Java程序时,一个虚拟机...

长沙千锋 ⋅ 05/17 ⋅ 0

14、Java并发性和多线程-Java ThreadLocal

以下内容转自http://ifeve.com/java-theadlocal/: Java中的ThreadLocal类可以让你创建的变量只被同一个线程进行读和写操作。因此,尽管有两个线程同时执行一段相同的代码,而且这段代码又有...

easonjim ⋅ 2017/06/16 ⋅ 0

CentOS 6.5 安装JDK(包含卸载原有默认JDK)

卸载原有1.7 JDK 查看是否安装了JDK 若有内容就进一步查看JDK信息 卸载 安装jdk ===================================== 安装wget 新建目录 进入目录 下载JDK 安装JDK 配置环境变量 往文件内...

阿白 ⋅ 05/23 ⋅ 0

Java学习---Java简单认识

前言 小编在学习Java方面的基础知识,发现里面有很多是结合之前的语言的特点发展过来的,不同的地方是,Java有它自己的发展和特点。下面小编先简单地做一下总结,结合看过的1-2章的J2SE视频,...

m18633778874 ⋅ 04/01 ⋅ 0

sharding-jdbc源码解析全集

sharding-jdbc源码解析之词法解析 sharding源码解析之api分析 sharding-jdbc源码解析之spring集成 sharding-jdbc源码解析之spring集成分片构造实现 sharding-jdbc源码解析之jdbc规范重写 sh...

天河2018 ⋅ 05/03 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

OSChina 周日乱弹 —— 这么好的姑娘都不要了啊

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @TigaPile :分享曾惜的单曲《讲真的》 《讲真的》- 曾惜 手机党少年们想听歌,请使劲儿戳(这里) @首席搬砖工程师 :怎样约女孩子出来吃饭,...

小小编辑 ⋅ 18分钟前 ⋅ 1

Jenkins实践3 之脚本

#!/bin/sh# export PROJ_PATH=项目路径# export TOMCAT_PATH=tomcat路径killTomcat(){pid=`ps -ef | grep tomcat | grep java|awk '{print $2}'`echo "tom...

晨猫 ⋅ 今天 ⋅ 0

Spring Bean的生命周期

前言 Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解。 首先看下生命周期图: 再谈生命周期之前有一点需要先明确: Spring 只帮我们管理单例模...

素雷 ⋅ 今天 ⋅ 0

zblog2.3版本的asp系统是否可以超越卢松松博客的流量[图]

最近访问zblog官网,发现zlbog-asp2.3版本已经进入测试阶段了,虽然正式版还没有发布,想必也不久了。那么作为aps纵横江湖十多年的今天,blog2.2版本应该已经成熟了,为什么还要发布这个2.3...

原创小博客 ⋅ 今天 ⋅ 0

聊聊spring cloud的HystrixCircuitBreakerConfiguration

序 本文主要研究一下spring cloud的HystrixCircuitBreakerConfiguration HystrixCircuitBreakerConfiguration spring-cloud-netflix-core-2.0.0.RELEASE-sources.jar!/org/springframework/......

go4it ⋅ 今天 ⋅ 0

二分查找

二分查找,也称折半查找、二分搜索,是一种在有序数组中查找某一特定元素的搜索算法。搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束;如果某一特定元素大于...

人觉非常君 ⋅ 今天 ⋅ 0

VS中使用X64汇编

需要注意的是,在X86项目中,可以使用__asm{}来嵌入汇编代码,但是在X64项目中,再也不能使用__asm{}来编写嵌入式汇编程序了,必须使用专门的.asm汇编文件来编写相应的汇编代码,然后在其它地...

simpower ⋅ 今天 ⋅ 0

ThreadPoolExecutor

ThreadPoolExecutor public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, ......

4rnold ⋅ 昨天 ⋅ 0

Java正无穷大、负无穷大以及NaN

问题来源:用Java代码写了一个计算公式,包含除法和对数和取反,在页面上出现了-infinity,不知道这是什么问题,网上找答案才明白意思是负的无穷大。 思考:为什么会出现这种情况呢?这是哪里...

young_chen ⋅ 昨天 ⋅ 0

前台对中文编码,后台解码

前台:encodeURI(sbzt) 后台:String param = URLDecoder.decode(sbzt,"UTF-8");

west_coast ⋅ 昨天 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部