使用时
Timer.schedule(TimerTask task, long delay, long period)
(即使用固定延迟执行),如果指定的 TimerTask
的 run()
方法花费的时间超过 period
会发生什么完全的?两个并发的 TimerTask
线程是否可能因此而运行?
如果是这样,有没有办法避免它?
最佳答案
定时器的documentation说如下:
Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.
也就是说,并发的 TimerTask
线程不会运行。这些任务将累积到一个队列中。这可能合适,也可能不合适(更有可能,不合适)。
关于java - 如果 TimerTask 的执行时间比指定的时间间隔长,会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2264476/