在Timer中调度Java TimerTask时,它是否已经“正在执行”? [英] When a Java TimerTask is scheduled in a Timer, is it already "executing"?

查看:284
本文介绍了在Timer中调度Java TimerTask时,它是否已经“正在执行”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清一下有关TimerTask的内容。如果您有以下代码:

I would like to clarify something about TimerTask. When you have the code below:

timer.schedule(task, 60000);

任务计划在接下来的1分钟内运行,任务对象是否已经执行?

where the task is scheduled to run in the next 1 minute, is the task object already executing?

因为我的代码中的某个地方我调用了task.cancel()但似乎调用没有阻止

because somewhere in my code I called task.cancel() but it seems that the call doesn't prevent

要执行的任务。我甚至记录了来自调用的返回值,它返回false。

task to be executed. I even logged the return value from the call and it returns false.

当我阅读取消方法的文档时,我想到了我的问题:

I came about my question when I read the documentation for the cancel method:


取消TimerTask并将其从Timer的队列中删除。
通常,如果调用没有阻止TimerTask
至少运行一次,则返回false。后续调用无效。如果调用阻止了计划执行,则返回true,否则返回false。

Cancels the TimerTask and removes it from the Timer's queue. Generally, it returns false if the call did not prevent a TimerTask from running at least once. Subsequent calls have no effect. Returns true if the call prevented a scheduled execution from taking place, false otherwise.

我相信我在1分钟之前调用了cancel()延迟。但是如何取消返回false,

I believe I called cancel() before the 1 minute delay. But how come cancel returned false,

它[任务]是否已经执行?

is it [task] already executing?

希望你能给我线索/提示甚至是对此的解释。谢谢!

Hope you can give me clues/hints or even an explanation to this. Thanks SO!

推荐答案


  • 任务计划在接下来的1分钟内运行,是已经执行的任务对象

    • where the task is scheduled to run in the next 1 minute, is the task object already executing
    • 不,它将调用运行这个任务的方法正好60秒。如果 task.cancel()返回 false ,这可能意味着3件事:

      No, it is going to invoke the run method of this task in exactly 60 seconds. If task.cancel() returns false, that could mean 3 things:


      • 任务被安排一次性执行并且已经运行OR

      • 任务从未被安排或

      • 任务已被取消或

      因此,如果您确定要拨打取消在安排任务后60秒之前,您可能会多次调用它,并从后续取消获得结果,或者您正在调用取消其他任务。

      Hence, if you are certain that you call cancel before 60 seconds after scheduling the task, you could potentially either call it several times, and get a result from a subsequent cancel, OR you are calling cancel on a different task.

      你可以实现一个所需的功能:

      You can achieve a desired functionality with:

      ScheduledExecutorService.schedule(callable,delay,timeunit)

      出现了优先考虑ScheduledExecutorService的原因此处

      Reasons why ScheduledExecutorService are is a preferred way are outlined here:


      • 计时器可能对系统时钟的变化很敏感,ScheduledThreadPoolExecutor不是

      • Timer can be sensitive to changes in the system clock, ScheduledThreadPoolExecutor isn't

      Timer只有一个执行线程,因此长时间运行的任务可以延迟其他任务。 ScheduledThreadPoolExecutor可以配置任意数量的线程。此外,您可以完全控制创建的线程,如果您需要(通过提供ThreadFactory)

      Timer has only one execution thread, so long-running task can delay other tasks. ScheduledThreadPoolExecutor can be configured with any number of threads. Furthermore, you have full control over created threads, if you want (by providing ThreadFactory)

      在TimerTask中抛出的运行时异常会杀死一个线程,从而使Timer死机:-( ...即计划任务将不再运行.ScheduledThreadExecutor不仅捕获运行时异常,而且它允许您根据需要处理它们(通过从ThreadPoolExecutor覆盖afterExecute方法)。抛出异常的任务将被取消,但其他任务将继续运行。

      runtime exceptions thrown in TimerTask kill that one thread, thus making Timer dead :-( ... i.e. scheduled tasks will not run anymore. ScheduledThreadExecutor not only catches runtime exceptions, but it lets you handle them if you want (by overriding afterExecute method from ThreadPoolExecutor). Task which threw exception will be canceled, but other tasks will continue to run.

      这篇关于在Timer中调度Java TimerTask时,它是否已经“正在执行”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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