Java Timer
Java Timer 定时类,主要用来执行定时任务
Timer管理所有要执行的定时任务
TimerTask封装好的定时任务
常见的用法
MyTask myTask = new MyTask();
Timer timer = new Timer();
timer.schedule(myTask, new Date(), 1000);
void |
schedule(TimerTask task, long delay) |
Schedules the specified task for execution after the specified delay.
|
|---|---|---|
void |
schedule(TimerTask task, long delay, long period) |
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
|
void |
schedule(TimerTask task, Date time) |
Schedules the specified task for execution at the specified time.
|
void |
schedule(TimerTask task, Date firstTime, long period) |
Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
|
void |
scheduleAtFixedRate(TimerTask task, long delay, long period) |
Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.
|
void |
scheduleAtFixedRate(TimerTask task,Date firstTime, long period) |
Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
|
摘自jdk,简单概括一下
schedule(TimerTask task, Date time):安排在指定的时间执行指定的任务。
schedule(TimerTask task, Date firstTime, long period) :安排指定的任务在指定的时间开始进行重复的固定延迟执行。
schedule(TimerTask task, long delay) :安排在指定延迟后执行指定的任务。
schedule(TimerTask task, long delay, long period) :安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。
scheduleAtFixedRate(TimerTask task, Date firstTime, long period):安排指定的任务在指定的时间开始进行重复的固定速率执行。
scheduleAtFixedRate(TimerTask task, long delay, long period):安排指定的任务在指定的延迟后开始进行重复的固定速率执行。
cancel()取消所有定时任务
再来看下TimerTask
| Modifier and Type | Method | Description |
|---|---|---|
boolean |
cancel() |
Cancels this timer task.
|
abstract void |
run() |
The action to be performed by this timer task.
|
long |
scheduledExecutionTime() |
Returns the scheduled execution time of the most recent actual execution of this task.
|
只有run()是抽象方法,需要重写
cancel()表示取消该定时任务
scheduledExecutionTime()表示最近的定时任务的实际执行时间
schedule()有个缺点,如果之前的线程花费时间较长,那么相应的之后的线程的执行时间也会相应延长
scheduleAtFixedRate()可以避免上述问题,schedule()侧重时间间隔的稳定,而scheduleAtFixedRate()侧重执行频率的稳定,即便前一个线程没有执行完,而下一个线程到了该执行的时间,也会开始下一个线程
Java Timer的更多相关文章
- Java Timer触发定时器
XML: <!-- Java Timer定时 --> <!-- <bean id="shortUrlTask" class=" com.sprin ...
- Java:利用java Timer类实现定时执行任务的功能
一.概述 在java中实现定时执行任务的功能,主要用到两个类,Timer和TimerTask类.其中Timer是用来在一个后台线程按指定的计划来执行指定的任务.TimerTask一个抽象类,它的子类代 ...
- JAVA Timer定时器使用方法(二)
JAVA Timer 定时器测试 MyTask.java:package com.timer; import java.text.SimpleDateFormat;import java.util. ...
- Java定时任务:利用java Timer类实现定时执行任务的功能
一.概述 在java中实现定时执行任务的功能,主要用到两个类,Timer和TimerTask类.其中Timer是用来在一个后台线程按指定的计划来执行指定的任务. TimerTask一个抽象类,它的子类 ...
- JAVA Timer定时器使用方法
JAVA Timer 定时器测试 MyTask.java:package com.timer; import java.text.SimpleDateFormat;import java.util. ...
- java Timer定时器管理类
1.java timer类,定时器类.启动执行定时任务方法是timer.schedule(new RemindTask(), seconds*1000);俩参数分别是TimerTask子类,具体执行定 ...
- Java Timer TimerTask Example(java Timer的例子)
Java java.util.Timer is a utility class that can be used to schedule a thread to be executed at cert ...
- java Timer 定时每天凌晨1点执行任务
import java.util.TimerTask;/** * 执行内容 * @author admin_Hzw * */public class Task extends TimerTask { ...
- java timer 执行任务
1. 建立timer import java.util.Timer; import java.util.TimerTask; public class Start { public class Sta ...
随机推荐
- spring boot 自定义视图路径
boot 自定义访问视图路径 . 配置文件 目录结构 启动类: html页面 访问: 覆盖boot默认路径引用. 如果没有重新配置,则在pom引用模板. 修改配置文件. 注意一定要编译工程
- 【powerBI】power pivot添加参数表
背景 最近在做应用分析,爬了几个应用市场的排行榜,需要分析前多少名各个品类的app有多少个.数据量不大,excel就能做分析,所以想起来pivot的参数表功能.这个功能还是比较实用的,在这再做个记录, ...
- 精通Web Analytics 2.0 (13) 第十一章:变身分析忍者的指导原则
精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第十一章:变身分析忍者的指导原则 这个激动人心的一章,分析了几乎所有工作的各个方面. 目标很简单:使用成熟的方法来帮助避免淹死的 ...
- 类库、委托、is/as
一.类库(Class Library) 定义:类库是一个综合性的面向对象的可重用类型集合,这些类型包括:接口.抽象类和具体类.(内容不可见) 类库建立:文件--新建--项目--类库--生成解决方案 类 ...
- JavaScript实现图片裁剪预览效果~(第一个小玩具)
感觉开始学习的前一个月真的太不珍惜慕课网的资源了 上面蛮多小玩意真的蛮适合我这样刚入门JavaScript的同学加深使用理解 大概收藏了百来门或大或小的课程 有一个感觉就是学这个真的比光是看书看概 ...
- 水仙花在python3在pycharm的实现
---恢复内容开始--- #方法一:#-*- coding: utf-8-*-while True: num = input("请输入一个三位数") num = int(num) ...
- centos6 安装 docker 问题
参考:https://www.cnblogs.com/cs294639693/p/10164258.html 第一步:删除 参考:https://www.cnblogs.com/liuyanshen ...
- Node 在 Centos7 系统下的安装
1,下载二进制包 https://nodejs.org/zh-cn/download/ 根据自己的需求选择对应的版本,不推荐使用源码包,容易出现错误 2,上传到 linux 服务器 3, 解 ...
- GridView Bind 短日期 格式
ASP.NET的GridView控件的编辑模板中,需要绑定数据库中的某个字段,如<%# Bind("startTime","{0:d}") %> 在 ...
- ThinkPHP输入验证和I方法使用
在Web开发过程中,我们经常需要获取系统变量或者用户提交的数据,这些变量数据错综复杂,而且一不小心就容易引起安全隐患,但是如果利用好ThinkPHP提供的变量获取功能,就可以轻松的获取和驾驭变量了. ...