我在使用timer来定时执行一个任务时,方法如下:
timer.scheduleAtFixedRate(task, firstTime, period)
需求如下:
task为一个生成文件的任务,首次执行任务时间firsttime为当前时间的下一个半点,比如现在时13:20,执行时间就为13:30,周期为30*60*1000,每半小时执行一次task出现的bug:
在系统繁忙时,在正常执行一段时间后,会出现每秒钟都执行多次任务,就是run方法被多次调用了,导致文件每秒钟都有生成。可从打出来的log看,period还是30*60*1000,没有错。个人疑问:timer是否存在bug。
timer.scheduleAtFixedRate(task, firstTime, period)
需求如下:
task为一个生成文件的任务,首次执行任务时间firsttime为当前时间的下一个半点,比如现在时13:20,执行时间就为13:30,周期为30*60*1000,每半小时执行一次task出现的bug:
在系统繁忙时,在正常执行一段时间后,会出现每秒钟都执行多次任务,就是run方法被多次调用了,导致文件每秒钟都有生成。可从打出来的log看,period还是30*60*1000,没有错。个人疑问:timer是否存在bug。
解决方案 »
- 在java工程中要解析一个xml文件要怎么写那个xml文件的路径
- applet在浏览器中运行出错问题
- 老师布置的一个java的狄杰斯特拉算法问题。高手请进。
- 批处理文件中的if errorlevel问题
- 如何打开Dic或者Dct格式文件
- Exception in thread "main" java.lang.NoClassDefFoundError: app 这个错误是什么意思?????
- 不是说静态的函数不能调用非静态的变量和函数吗 为什么这个程序编译通过了呢?
- 急!在java中请问在不显示对话框的情况下,如何打开一个文件(不一定只是文本文件)?
- 什么叫超类?
- 大家帮我出出主意!
- java画图怪事之二!!!!
- 谁有java swing 方面的电子书,能提供一下吗。??
不要动不动就说是人家的问题,先看看人家的说明、自己的用法
看看传参是否有误
或许,是你调用的TimerTask 内实现的有问题
贴点代码出来public void scheduleAtFixedRate(TimerTask task,
Date firstTime,
long period)Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.
In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate). Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another. Parameters:
task - task to be scheduled.
firstTime - First time at which task is to be executed.
period - time in milliseconds between successive task executions.