Timer 【中文翻译小组】

  • Timer    定时器

    public class Timer extends Object 

  • 公共类

  •  

    java.lang.Object
       ↳java.util.Timer
    所属包:java.util.Timer

     

  • A facility for threads to schedule tasks for future execution in a background thread.

  • Timer是运行在后台线程中,调度未执行的任务的类。

  • Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.

  •  

    任务可以安排为一次性执行,也可以安排为定期重复执行。

     

    Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks,
  • 对应于每个计时器对象的是一个单独的后台线程,用于按顺序执行计时器的所有任务。

  • sequentially. 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.

  •  

    当(如果)违规任务最终完成时,后续任务可能会“扎堆”并快速连续执行。

    After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the timer's cancel method.
  •  

    当对 timer 对象的最后一次活动引用消失并且所有未完成的任务都完成执行之后,timer 的任务执行线程将优雅地终止(并进入垃圾收集)。 然而,这可能需要任意长的时间才能发生。 默认情况下,任务执行线程不作为守护进程线程运行,因此它能够阻止应用程序终止。 如果调用方希望快速终止计时器的任务执行线程,则调用方应调用计时器的 cancel 方法。

    If the timer's task execution thread terminates unexpectedly, for example, because its stop method is invoked, any further attempt to schedule a task on the timer will result in an IllegalStateException, as if the timer's cancel method had been invoked.

    This class is thread-safe: multiple threads can share a single Timer object without the need for external synchronization.

    This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method.

    Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn't require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.

    Implementation note: This class scales to large numbers of concurrently scheduled tasks (thousands should present no problem). Internally, it uses a binary heap to represent its task queue, so the cost to schedule a task is O(log n), where n is the number of concurrently scheduled tasks.

    Implementation note: All constructors start a timer thread.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值