Java中的resettable超时 [英] resettable timeout in Java

查看:149
本文介绍了Java中的resettable超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(类似于可重置的Java计时器,但我需要探索一些细微之处)

(similar to "Resettable Java Timer" but there are some subtleties I need to explore)

我需要一个可重置的超时功能,这样如果我的类在时间间隔T0内没有执行特定动作(其中T0在50-1000毫秒附近) ,然后调用一个方法:

I need a resettable timeout feature, so that if my class does not perform a particular action within an interval of time T0 (where T0 is in the neighborhood of 50-1000msec), then a method gets called:

class MyClass {
    static final private timeoutTime = 50;
    final private SomeTimer timer = new SomeTimer(timeoutTime, 
        new Runnable () { public void run() {
            onTimeout();
        }});

    private void onTimeout() { /* do something on timeout */ }

    public void criticalMethod() { this.timer.reset(); }
}

我可以用什么来实现这个?我熟悉 ScheduledExecutorService ,以及调用 ScheduledFuture.cancel(),然后重新安排任务似乎就像它应该工作,但是如果cancel()失败并且计划任务在应该执行时存在潜在危险T。我觉得我在这里错过了一个微妙之处。

What can I use to implement this? I'm familiar with ScheduledExecutorService, and the idea of calling ScheduledFuture.cancel() and then rescheduling the task seems like it should work, but then there's a potential hazard if cancel() fails and the scheduled task executes when it shouldn't. I feel like I'm missing a subtlety here.

另外(也许更重要的是),有没有办法测试我的实现/证明它正常工作?

Also (perhaps more importantly), is there a way to test my implementation / prove that it works properly?

编辑:我特别关注经常调用 criticalMethod()的情况(或许每毫秒几次)...如果我使用ScheduledExecutorService,它似乎是一个潜在的资源问题,不断创建新的计划任务+取消旧计划任务,而不是直接重新安排任务。

edit: I am particularly concerned about the case where criticalMethod() gets called often (perhaps several times per millisecond)... if I use ScheduledExecutorService, it just seems like a potential resource problem to keep creating new scheduled tasks + canceling old ones, rather than having a direct way to reschedule a task.

推荐答案

取消的属性附加到任务对象。因此,当您调用取消时,任务尚未开始,并且它将无法运行;或者当你打电话给取消时,任务已经开始,并且它被中断。

The cancelled attribute is attached to the task object. So, either the task hasn't started when you call cancel, and it won't get run; or the task has already started when you call cancel, and it gets interrupted.

如何处理中断给你。你应该定期轮询 Thread.interrupted()(顺便说一句,重置中断的标志,所以要小心)如果你没有调用任何可中断的函数(声明的那些) InterruptedException 在他们的投掷子句中。

How to handle interruption is up to you. You should regularly poll Thread.interrupted() (which, by the way, resets the interrupted flag, so beware) if you aren't calling any interruptible function (ones that declare InterruptedException in their throws clause).

当然,如果你正在调用这样的函数,你应该明智地处理 InterruptedException (包括重新断言被中断的标志( Thread.currentThread()。interrupt())在你的任务返回之前)。 : - )

Of course, if you are calling such functions, you should handle InterruptedException sensibly (which includes reasserting the interrupted flag (Thread.currentThread().interrupt()) before your task returns). :-)

要回答您的编辑,只要您的对象没有很多状态,对象创建就很便宜。我个人不会太担心它,除非分析显示它是一个瓶颈。

To answer your edit, object creation is cheap, as long as your object doesn't have a lot of state. I personally wouldn't worry about it too much unless profiling shows it to be a bottleneck.

这篇关于Java中的resettable超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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