如果TimerTask执行的时间比指定的时间间隔长,会发生什么? [英] What happens if a TimerTask takes longer to execute than the specified interval?

查看:297
本文介绍了如果TimerTask执行的时间比指定的时间间隔长,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用时

Timer.schedule(TimerTask任务,长延迟,长时间段)

(即使用固定延迟执行),如果指定的 TimerTask run()会发生什么方法需要超过期限才能完成?是否可能因为这个而运行两个并发的 TimerTask 线程?
如果是这样,有没有办法避免它?

(i.e. with fixed-delay execution), what happens if the specified TimerTask's run() method takes longer than period to complete? Is it possible that two concurrent TimerTask threads will be running because of this? And if so, is there a way to avoid it?

推荐答案

计时器的文档说明如下:


计时器任务应该快速完成。如果计时器任务需要花费过多的时间来完成,它会占用计时器的任务执行线程。反过来,这可以延迟后续任务的执行,这些后续任务可以在紧急任务最终完成时(以及如果)快速连续地聚集并执行。

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 线程将不会运行。任务将累积到队列中。这可能是也可能不合适(更有可能,不是)。

That is, concurrent TimerTask threads will not be running. The tasks will accumulate into a queue. This may or may not be appropriate (more likely, not).

这篇关于如果TimerTask执行的时间比指定的时间间隔长,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆