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 ...
随机推荐
- 2.HTML+CSS制作一闪一闪亮晶晶的星星(stars)
效果地址:https://codepen.io/flyingliao/pen/NJxbdB?editors=1100 HTML code: <div class="stars" ...
- WebForm(Application,ViewState,Repeater的Command操作)
一.AppliCation: 1.存储在服务器端,占用服务器内存 2.生命周期:永久 3.所有人都可访问的共有对象,一般用作服务器缓存 4.赋值:Application["key" ...
- VideoView播放视频——Android
Android为开发人员提供了一种简单的播放视频媒体的方式,那就是VideoView,本篇博客就来讲讲VideoView如何播放视频,最后将以一个简单的Demo演示. VideoView VideoV ...
- maven项目里的mapper不被加载,解析
出现这个错误是因为maven加载配置文件是从resource里加载的,所以要配置一下
- JS 异步分段上传文件
为了解决大文件上传 (PHP上传最大限制2GB) 同时为了解决文件上传是对服务器造成的压力 可以通过分段上传解决这个问题,这得益于HTML5开发的file API 前台代码: 引用了进度条插件myPr ...
- 【Source Insight 】之marco学习笔记2
现在我们看先看一个 官方地址https://www.sourceinsight.com/download/macro-files/中的 autoexp.em Automatically expands ...
- springboot 端口号
1. 读取端口号 2.多端口运行 2.
- Nginx代理webSocket经常中断的解决方案, 如何保持长连接
背景 这天气够热的,要处理的事情也够多的.... 1 2 想看解决的,直接 ctrl+f搜索关键字‘配置点’ 开始前交代(想看原因的看这个,个人观点,不代表正确) 解说:今天用nginx反代通讯项目, ...
- mui页面交互
1.页面a准备函数 function hideBackBtn() { // $('.menua').removeClass('mui-icon-back').addClass('mui-icon-ba ...
- Net操作Excel_NPOI
Npoi 简介 1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet:行:Row:单元格Cell. 2.Npoi 下载地址:http://npoi.codep ...