java - 如果 TimerTask 的执行时间比指定的时间间隔长,会发生什么?

标签 java multithreading timer

使用时

Timer.schedule(TimerTask task, long delay, long period)

(即使用固定延迟执行),如果指定的 TimerTaskrun() 方法花费的时间超过 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/

相关文章:

java - Quartz 作业已存在,名称/组带有 spring mvc 和 Camel

java - 如何防止 apache http 客户端遵循重定向

c - 在 Visual Studio 中测量线程的最大堆栈使用率

C-kill系统调用不会唤醒进程

performance - Flutter,Future.delay 或 Timer 性能更好?

matlab - 将 Matlab 计时器对象重置为时间 0

javascript - 定时器开始暂停和恢复

java - 更改 PDF 旋转文本的字体

java - 由于 "no"原因扩展类,java 中的性能是否会下降?

Java 8 非阻塞读取有竞争条件?