简体   繁体   English

Java:它创建的计时器和线程

[英]Java: Timer and thread it creates

I have this question: 我有这个问题:

I have a timer. 我有一个计时器。 With scheduleAtFixedRate it creates a new Timer task. 使用scheduleAtFixedRate,它会创建一个新的Timer任务。 In that timer task there is certain code, which may take a while to complete. 在该计时器任务中有某些代码,可能需要一段时间才能完成。 How can I make sure that Timer won't create new task when the previous one didn't complete yet? 如何在前一个任务尚未完成时确保Timer不会创建新任务?

Thanks 谢谢

My answer would be to not to use Timer , it's obsolete. 我的答案是不使用Timer ,它已经过时了。 Since Java5, Timer has been superseded by the ScheduledExecutorService , which is much more flexible and easier to use. 自Java5以来, Timer已被ScheduledExecutorService取代,后者更灵活,更易于使用。 You get finer control over how the scheduler works, the sort of control you don't get with Timer . 您可以更好地控制调度程序的工作方式,即Timer无法实现的控制。

You create one using the Executors factory class, which has a number of factory methods. 您可以使用Executors工厂类创建一个工厂类,该工厂类具有许多工厂方法。 The one you should be looking at is newSingleThreadScheduledExecutor , which should do exactly what you're looking for: 您应该关注的是newSingleThreadScheduledExecutor ,它应该完全符合您的要求:

Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically. 创建一个单线程执行程序,可以调度命令在给定的延迟后运行,或者定期执行。 Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. 保证任务按顺序执行,并且在任何给定时间不会有多个任务处于活动状态。

With a ScheduledExecutorService , instead of subclassing TimerTask , you subclass Runnable directly, and then submit the task to the executor. 使用ScheduledExecutorService ,而不是子类化TimerTask ,直接将Runnable子类化,然后将任务提交给执行程序。 There are various methods on the executor, you need to pick which one is suitable for your needs (read the javadoc for ScheduledExecutorService carefully), but the gist is something like this: 执行程序有各种方法,你需要选择哪一种方法适合你的需要(仔细阅读ScheduledExecutorService的javadoc),但要点是这样的:

    // initialise the executor
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

    while (tasksRemaining) {
        // create your task
        Runnable task = ....;
        // submit it to the executor, using one of the various scheduleXYZ methods
        executor.schedule(task, delay, unit);
    }

    // when everything is finished, shutdown the executor
    executor.shutdown();

As always, read the javadoc. 像往常一样,阅读javadoc。

The doc for the Timer class Timer类的doc

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. 对应于每个Timer对象的是一个后台线程,用于按顺序执行所有计时器的任务。 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. 反过来,这可以延迟后续任务的执行,后续任务可以在紧急任务最终完成时(以及如果)快速连续地“聚集”并执行。

suggests that there's only one background thread. 表明只有一个后台主题​​。 So I believe this scenario won't occur. 所以我相信这种情况不会发生。

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

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