博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5、Java的Timer定时器
阅读量:7106 次
发布时间:2019-06-28

本文共 3198 字,大约阅读时间需要 10 分钟。

hot3.png

1、介绍

在java中实现定时任务有几种方式:

  • 1、自己写代码,解析cron表达式,或者sleep多长时间在执行一次

  • 2、使用内置的timer类来实现

  • 3、使用ScheduledThreadPoolExecutor(和timer有区别,timer一个任务抛异常,所有任务都不能用了)

  • 3、利用开源的quartz,结合spring进行使用

下面重点讲解Timer

2、代码

public class TimerTest {    public static void main(String[] args) {        Timer timer = new Timer();        timer.schedule(new TimerTask() {  //public abstract class TimerTask implements Runnable            [@Override](https://my.oschina.net/u/1162528)            public void run() {                //Fixme Do SameThing                System.out.println("Task1");            }        }, 1000, 1 * 1000);        timer.schedule(new TimerTask() {            [@Override](https://my.oschina.net/u/1162528)            public void run() {                //fixme 一个timer可以启动多个定时任务,他里面维护了一个queue                //fixme private final TaskQueue queue = new TaskQueue();                //fixme private final TimerThread thread = new TimerThread(queue);                //fixme queue.add(task);                System.out.println("Task2");            }        }, 200, 2 * 1000);    }    /**     * TimerThread 的run方法调用mainLoop     *///    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) {//            }//        }//    }}

3、Timer内部运作图

转载于:https://my.oschina.net/liufukin/blog/2221789

你可能感兴趣的文章
跟我学习dubbo-简介(1)
查看>>
Rsync基本操作加实时同步演练
查看>>
第1章 选择流程控制语句
查看>>
Java之品优购课程讲义_day03(8)
查看>>
EasySchedule定时任务web平台
查看>>
这10道springboot常见面试题你需要了解下
查看>>
大数据学习知识点导图
查看>>
openStack controller 管理网口TX数据量非常大 网络总是丢包
查看>>
CodeMix使用教程(三):Emmet
查看>>
ubuntu cenots 禁止本地登陆
查看>>
Windows server 2008R2升级到Windows server 2016
查看>>
配置方案:Redis持久化RDB和AOF
查看>>
linux云计算
查看>>
织梦修改memberlist标签调用自定义会员模型的会员信息
查看>>
手持机就是对讲机它们有什么区别
查看>>
图片批处理FastStone Photo Resizer
查看>>
C入门程序整体框架图
查看>>
Oracle八大性能视图之v$session
查看>>
junit 实例
查看>>
mount 命令详解
查看>>