jdk自带定时器使用方法详解
发布时间 - 2026-01-11 02:05:50 点击率:次首先看一下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
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】
Swift中循环语句中的转移语句 break 和 continue
Laravel怎么实现支付功能_Laravel集成支付宝微信支付
如何快速登录WAP自助建站平台?
*服务器网站为何频现安全漏洞?
Laravel如何处理表单验证?(Requests代码示例)
湖南网站制作公司,湖南上善若水科技有限公司做什么的?
Laravel怎么使用Markdown渲染文档_Laravel将Markdown内容转HTML页面展示【实战】
Laravel项目结构怎么组织_大型Laravel应用的最佳目录结构实践
Laravel怎么在Controller之外的地方验证数据
如何用PHP快速搭建CMS系统?
猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?
Laravel如何与Pusher实现实时通信?(WebSocket示例)
Laravel观察者模式如何使用_Laravel Model Observer配置
Laravel如何创建自定义Facades?(详细步骤)
,网页ppt怎么弄成自己的ppt?
Laravel如何使用Vite进行前端资源打包?(配置示例)
海南网站制作公司有哪些,海口网是哪家的?
Laravel如何记录日志_Laravel Logging系统配置与自定义日志通道
IOS倒计时设置UIButton标题title的抖动问题
桂林网站制作公司有哪些,桂林马拉松怎么报名?
Laravel如何使用Sanctum进行API认证?(SPA实战)
香港服务器建站指南:免备案优势与SEO优化技巧全解析
网站制作软件有哪些,制图软件有哪些?
,南京靠谱的征婚网站?
Bootstrap整体框架之JavaScript插件架构
JS弹性运动实现方法分析
头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?
Laravel如何使用Contracts(契约)进行编程_Laravel契约接口与依赖反转
laravel怎么为应用开启和关闭维护模式_laravel应用维护模式开启与关闭方法
Android仿QQ列表左滑删除操作
如何在景安服务器上快速搭建个人网站?
如何在IIS中新建站点并配置端口与IP地址?
iOS正则表达式验证手机号、邮箱、身份证号等
关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)
Laravel如何使用Socialite实现第三方登录?(微信/GitHub示例)
Laravel Docker环境搭建教程_Laravel Sail使用指南
免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?
Laravel如何使用集合(Collections)进行数据处理_Laravel Collection常用方法与技巧
Java Adapter 适配器模式(类适配器,对象适配器)优缺点对比
Python数据仓库与ETL构建实战_Airflow调度流程详解
悟空识字怎么关闭自动续费_悟空识字取消会员自动扣费步骤
企业网站制作这些问题要关注
javascript读取文本节点方法小结
手机软键盘弹出时影响布局的解决方法
Windows10如何删除恢复分区_Win10 Diskpart命令强制删除分区
如何在浏览器中启用Flash_2025年继续使用Flash Player的方法【过时】
Laravel怎么清理缓存_Laravel optimize clear命令详解
如何用5美元大硬盘VPS安全高效搭建个人网站?
Laravel如何发送邮件和通知_Laravel邮件与通知系统发送步骤

