Java任务调度Timer定时器怎么实现
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章主要介绍"Java任务调度Timer定时器怎么实现",在日常操作中,相信很多人在Java任务调度Timer定时器怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家
千家信息网最后更新 2025年12月02日Java任务调度Timer定时器怎么实现
这篇文章主要介绍"Java任务调度Timer定时器怎么实现",在日常操作中,相信很多人在Java任务调度Timer定时器怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Java任务调度Timer定时器怎么实现"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
Timer任务调用相关类的UML图如下(仅包含部分变量与方法):
使用时,需自己写一个类,继承TimerTask,重写run()方法,大致步骤如下:
1.Timer的成员变量thread初始化时会聚合queue
public class Timer { /** * The timer task queue. This data structure is shared with the timer * thread. The timer produces tasks, via its various schedule calls, * and the timer thread consumes, executing timer tasks as appropriate, * and removing them from the queue when they're obsolete. */ private final TaskQueue queue = new TaskQueue(); /** * The timer thread. */ private final TimerThread thread = new TimerThread(queue);2.Timer构造方法有几个重载,最终会调用thread的start()方法,启动线程,调用run()方法
public Timer(String name) { thread.setName(name); thread.start();}3.thread的run()方法核心逻辑为mainLoop()方法:如果queue中TimerTask[]为空 && newTasksMayBeScheduled = true,则queue.wait()进入阻塞状态
private void mainLoop() { while (true) { try { TimerTask task; boolean taskFired; synchronized(queue) { // Wait for queue to become non-empty while (queue.isEmpty() && newTasksMayBeScheduled) queue.wait(); if (queue.isEmpty()) break; // Queue is empty and will forever remain; die // Queue nonempty; look at first evt and do the right thing long currentTime, executionTime; task = queue.getMin(); synchronized(task.lock) { if (task.state == TimerTask.CANCELLED) { queue.removeMin(); continue; // No action required, poll queue again } currentTime = System.currentTimeMillis(); executionTime = task.nextExecutionTime; if (taskFired = (executionTime<=currentTime)) { if (task.period == 0) { // Non-repeating, remove queue.removeMin(); task.state = TimerTask.EXECUTED; } else { // Repeating task, reschedule queue.rescheduleMin( task.period<0 ? currentTime - task.period : executionTime + task.period); } } } if (!taskFired) // Task hasn't yet fired; wait queue.wait(executionTime - currentTime); } if (taskFired) // Task fired; run it, holding no locks task.run(); } catch(InterruptedException e) { } } }4.阻塞状态下,调用Timer类的schedule方法开启定时任务,schedule有几个重载,最终都是调用sched方法
5.sched方法中,queue.add(task),然后queue.notify()唤醒thread的线程
private void sched(TimerTask task, long time, long period) { if (time < 0) throw new IllegalArgumentException("Illegal execution time."); // Constrain value of period sufficiently to prevent numeric // overflow while still being effectively infinitely large. if (Math.abs(period) > (Long.MAX_VALUE >> 1)) period >>= 1; synchronized(queue) { if (!thread.newTasksMayBeScheduled) throw new IllegalStateException("Timer already cancelled."); synchronized(task.lock) { if (task.state != TimerTask.VIRGIN) throw new IllegalStateException( "Task already scheduled or cancelled"); task.nextExecutionTime = time; task.period = period; task.state = TimerTask.SCHEDULED; } queue.add(task); if (queue.getMin() == task) queue.notify(); }}6.therad的run()继续执行,进行一系列判断,最后调用TimerTask的run()方法,执行定时任务(见mainLoop方法)
到此,关于"Java任务调度Timer定时器怎么实现"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
方法
任务
定时器
调度
学习
变量
更多
状态
线程
帮助
阻塞
实用
接下来
成员
文章
核心
步骤
理论
知识
篇文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
cf外挂导致服务器暂停
上海推广网络技术互惠互利
远程服务器预览附件路径怎么写
南昌服务器机柜价格
王者服务器选哪个区人最多
数据库收缩什么原理
服务器停止运行提示
安卓app网络安全
oracle数据库的存储过程
开一家软件开发公司
服务器硬盘上的数据安全吗
北京企业软件开发服务至上
网络安全手抄报排版设计
数据库关联表 bll
实用的外贸软件开发
网络安全小报手抄报内容
永嘉县互联网科技有限公司
网络安全电子产品
代理服务器 合法吗
speedtest测速服务器地址
鲲鹏平台软件开发
云服务器不安全
传奇服务器设置电信
企业软件开发定制公司哪家好
腾讯云的云服务器是不是可以运行
网络安全靠人民安全手抄报
数据库关联表 bll
小星精通网络技术
云端服务器已断开是什么意思
软件开发方向报告