设为首页 加入收藏

TOP

Java关于Timer schedule执行定时任务
2014-11-24 11:15:11 来源: 作者: 【 】 浏览:0
Tags:Java 关于 Timer schedule 执行 定时 任务

公司技术人员在实现内部办公系统与外部网站一体化的时候,最重要的步骤就是从OA系统读取数据,并且根据网站模板生成最终的静态页面。这里就需要一个定时任务,循环的执行。


技术人员在写定时任务的时候,想当然的以为Timer.schedule(TimerTask task, long delay)就是重复的执行task。程序运行后发现只运行了一次,总觉得是task里的代码有问题,花了很长时间调试代码都没有结果。


仔细研读java api,发现:


schedule(TimerTask task, long delay)的注释:Schedules the specified task for execution after the specified delay。大意是在延时delay毫秒后执行task。并没有提到重复执行


schedule(TimerTask task, long delay, long period)的注释:Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay。大意是在延时delay毫秒后重复的执行task,周期是period毫秒。


这样问题就很明确schedule(TimerTask task, long delay)只执行一次,schedule(TimerTask task, long delay, long period)才是重复的执行。关键的问题在于程序员误以为schedule就是重复的执行,而没有仔细的研究API,一方面也是英文能力不够,浏览API的过程中不能很快的理解到含义。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 比Timer更好方法 下一篇Android通过Location API显示地址..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)