简体   繁体   English

延迟为0时,Timer.schedule计划调度多长时间?

[英]How long does Timer.schedule schedule for when delay is 0?

In Timer.schedule(TimerTask task, long delay) , it says it will throw if delay is negative, but doesn't say anything about if delay is zero. Timer.schedule(TimerTask task, long delay) ,它表示如果delay为负,则将抛出该Timer.schedule(TimerTask task, long delay) ,但是如果delay为零,则不进行任何说明。 What will happen? 会发生什么? I tried on openjdk and it ran instantly. 我尝试使用openjdk,它立即运行。 Is this behavior specified somewhere else, or is it undefined (eg it means infinite on other implementations, or some implementations will do infinite sometimes and instant sometimes)? 这个行为是在其他地方指定的还是未定义的(例如,这意味着在其他实现上是无限的,或者某些实现有时会是无限的,有时会是瞬时的)?

From the Java SE7 documentation : Java SE7文档中

If delay is less than or equal to zero, the timer fires as soon as it is started 如果延迟小于或等于零,则计时器将在启动后立即触发

So the result you got is the expected behavior. 因此,您得到的结果是预期的行为。

I believe a Timer in java is a Thread with a task queue. 我相信Java中的Timer是带有任务队列的线程。 Items are ordered in the task queue by when they are supposed to fire. 项目应在应该触发时在任务队列中排序。 Tasks with a delay of 0 are supposed to fire immediately and thus go to top of queue. 延迟为0的任务应该立即触发,因此进入队列顶部。 I believe the behavior you are seeing is expected and should be consistent cross platform and across different jdks 我相信您所看到的行为是预期的,并且应该跨平台和跨不同的jdk保持一致

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM