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 ...
随机推荐
- nginx面试中最常见的18道题
1.请解释一下什么是Nginx? Nginx是一个web服务器和反向代理服务器,用于HTTP.HTTPS.SMTP.POP3和IMAP协议. 2.请列举Nginx的一些特性. Nginx服务器的特性包 ...
- 8.Appium的基本使用-1
1.Appium是什么?Appium是一个跨平台移动端自动化测试工具,可以便捷的为ios和Android平台创建自动化测试用例.模拟app内部各种操作,点击.滑动.文本输入等,手工操作的动作appiu ...
- angular.js前端分层开发(页面和js代码分离,并将js代码分层)
一. 抽取模块成base.js文件// 定义模块: var app = angular.module("eshop",['pagination']); 二. 抽取服务成brandS ...
- Windows 端口占用解决
- ORM一对多增加记录
多表操作(一对多)增加记录: 1)Bookl.objects.creat(name='python', price=77, publish_id=2) 2) pulish_obj = Publish. ...
- cxGrid 颜色设置
一.cxGrid 根据列值变色(样式) 在使用cxGrid的过程中,某一个单元格经常需要根据其他单元格的值来做相应的变色,如: 在cxGridDBTableView中,选定要变样式(如背景色.字体属性 ...
- ThinkPHP5分页样式设置
手册上讲分页类的使用时对样式讲的不够详细,这里我结合个人的摸索给大家一些参考意见. config里的分页配置我使用的是系统默认的bootstrap,查看thinkphp\library\think\p ...
- Linux命令:sshpass
sshpass介绍 sshpass是一款凡是为凡是使用ssl方式访问的操作提供一个免输入密码的非交互式操作,以便于在脚本中执行ssl操作,如ssh,scp等.sshpass是一家以色列公司Lingnu ...
- 一个未完成的2.6.32-220内核踩内存crash分析记录
遇到一个crash,log如下: BUG: unable to handle kernel NULL pointer dereference at (null) IP: [<ffffffff81 ...
- 转: python requests的安装与简单运用
requests是Python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢? 官方文档中是这样说明的: python的标准库urlli ...