首先看一下jdk自带定时器:

一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。与每个 Timer 对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。如果完成某个计时器任务的时间太长,那么它会“独占”计时器的任务执行线程。因此,这就可能延迟后续任务的执行,而这些任务就可能“堆在一起”,并且在上述不友好的任务最终完成时才能够被快速连续地执行。
schedule(TimerTask task,long delay) 安排在指定延迟后执行指定的任务。
schedule(TimerTask task,Date time) 安排在指定的时间执行指定的任务。如果此时间已过去,则安排立即执行该任务。
schedule(TimerTask task, long delay, long period) 安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟
schedule(TimerTask task,Date firstTime,long period) 安排指定的任务在指定的时间开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟。
package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* jdk自带定时器
*
* @author LIUTIE
*
*/
public class JDKTimer {
public static void main(String[] args) throws ParseException {
//日期格式工具
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timer timer = new Timer();
// 10s后执行定时器,仅执行一次
System.out.print(sdf.format(new Date()));
System.out.println("the timer one will be executed after 10 seconds...");
long milliseconds = 10 * 1000;
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer one has finished execution");
}
}, milliseconds);
//12秒后执行定时器,每1s执行一次
System.out.print(sdf.format(new Date()));
System.out.println("the timer two will be executed after 12 seconds...");
//启动后延迟时间
long afterSs = 12 * 1000;
//执行周期
long intervalSs1 = 1 * 1000;
timer.schedule(new TimerTask() {
// 执行计数器
int i = 0;
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer two has execution " + (++i) + " timers");
// 执行10次后关闭定时器
if (i == 10) {
this.cancel();
}
}
}, afterSs, intervalSs1);
// 指定时间执行定时器,仅执行一次
System.out.print(sdf.format(new Date()));
System.out.println("the timer three will be executed at 2017-06-27 21:47:00...");
Date date = sdf.parse("2017-06-27 21:47:00");
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer three has finished execution");
}
}, date);
// 从指定时间开始周期性执行
System.out.print(sdf.format(new Date()));
System.out.println("the timer four will be executed at 2017-06-27 21:48:00...");
// 执行间隔周期
long intervalSs = 1 * 1000;
// 开始执行时间
Date beginTime = sdf.parse("2017-06-27 21:48:00");
timer.schedule(new TimerTask() {
// 执行计数器
int i = 0;
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer four has execution " + (++i) + " timers");
// 执行10次后关闭定时器
if (i == 10) {
this.cancel();
}
}
}, beginTime, intervalSs);
}
}
执行结果
2017-06-27 21:46:24the timer one will be executed after 10 seconds... 2017-06-27 21:46:24the timer two will be executed after 12 seconds... 2017-06-27 21:46:24the timer three will be executed at 2017-06-27 21:47:00... 2017-06-27 21:46:24the timer four will be executed at 2017-06-27 21:48:00... 2017-06-27 21:46:34the timer one has finished execution 2017-06-27 21:46:36the timer two has execution 1 timers 2017-06-27 21:46:37the timer two has execution 2 timers 2017-06-27 21:46:38the timer two has execution 3 timers 2017-06-27 21:46:39the timer two has execution 4 timers 2017-06-27 21:46:40the timer two has execution 5 timers 2017-06-27 21:46:41the timer two has execution 6 timers 2017-06-27 21:46:42the timer two has execution 7 timers 2017-06-27 21:46:43the timer two has execution 8 timers 2017-06-27 21:46:44the timer two has execution 9 timers 2017-06-27 21:46:45the timer two has execution 10 timers 2017-06-27 21:47:00the timer three has finished execution 2017-06-27 21:48:00the timer four has execution 1 timers 2017-06-27 21:48:01the timer four has execution 2 timers 2017-06-27 21:48:02the timer four has execution 3 timers 2017-06-27 21:48:03the timer four has execution 4 timers 2017-06-27 21:48:04the timer four has execution 5 timers 2017-06-27 21:48:05the timer four has execution 6 timers 2017-06-27 21:48:06the timer four has execution 7 timers 2017-06-27 21:48:07the timer four has execution 8 timers 2017-06-27 21:48:08the timer four has execution 9 timers 2017-06-27 21:48:09the timer four has execution 10 timers
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# jdk
# 定时器
# java当中的定时器的4种使用方式
# JAVA中 Spring定时器的两种实现方式
# java使用TimerTask定时器获取指定网络数据
# Java 定时器(Timer
# TimerTask)详解及实例代码
# 解析Java中的定时器及使用定时器制作弹弹球游戏的示例
# Java 定时器(Timer)及线程池里使用定时器实例代码
# java实现多线程之定时器任务
# java Quartz定时器任务与Spring task定时的几种实现方法
# Java定时器Timer简述
# Java中Spring使用Quartz任务调度定时器
# 计时器
# 将被
# 或其他
# 自带
# 排在
# 的是
# 执行时间
# 这就
# 看一下
# 它会
# 时才
# 太长
# 相对应
# 大家多多
# 不友好
# 延迟时间
# 在上述
# import
# test
# ParseException
相关文章:
如何在建站主机中优化服务器配置?
Bpmn 2.0的XML文件怎么画流程图
如何在Golang中引入测试模块_Golang测试包导入与使用实践
建站之星代理平台如何选择最佳方案?
如何快速搭建二级域名独立网站?
如何通过老薛主机一键快速建站?
如何安全更换建站之星模板并保留数据?
如何用低价快速搭建高质量网站?
陕西网站制作公司有哪些,陕西凌云电器有限公司官网?
在线制作视频的网站有哪些,电脑如何制作视频短片?
音乐网站服务器如何优化API响应速度?
企业网站制作费用多少,企业网站空间一般需要多大,费用是多少?
合肥做个网站多少钱,合肥本地有没有比较靠谱的交友平台?
如何用腾讯建站主机快速创建免费网站?
制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?
制作网站的软件免费下载,免费制作app哪个平台好?
建站之星与建站宝盒如何选择最佳方案?
如何选择美橙互联多站合一建站方案?
制作证书网站有哪些,全国城建培训中心证书查询官网?
C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换
制作网站的过程怎么写,用凡科建站如何制作自己的网站?
建站之星各版本价格是多少?
专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?
在线教育网站制作平台,山西立德教育官网?
如何选择适合PHP云建站的开源框架?
TestNG的testng.xml配置文件怎么写
黑客如何通过漏洞一步步攻陷网站服务器?
公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?
美食网站链接制作教程视频,哪个教做美食的网站比较专业点?
常州自助建站费用包含哪些项目?
建站之星如何修改网站生成路径?
如何做网站制作流程,*游戏网站怎么搭建?
网站制作公司排行榜,抖音怎样做个人官方网站
建站上传速度慢?如何优化加速网站加载效率?
如何构建满足综合性能需求的优质建站方案?
学校为何禁止电信移动建设网站?
网站建设制作需要多少钱费用,自己做一个网站要多少钱,模板一般多少钱?
网站制作新手教程,新手建设一个网站需要注意些什么?
linux top下的 minerd 木马清除方法
如何高效利用亚马逊云主机搭建企业网站?
黑客如何利用漏洞与弱口令入侵网站服务器?
c# F# 的 MailboxProcessor 和 C# 的 Actor 模型
如何在建站之星网店版论坛获取技术支持?
小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化
如何挑选优质建站一级代理提升网站排名?
如何选择靠谱的建站公司加盟品牌?
小程序网站制作需要准备什么资料,如何制作小程序?
如何高效完成自助建站业务培训?
建站之星安装需要哪些步骤及注意事项?
如何在云主机快速搭建网站站点?
*请认真填写需求信息,我们会在24小时内与您取得联系。