Note that there are some explanatory texts on larger screens.

plurals
  1. POresettable timeout in Java
    primarykey
    data
    text
    <p>(similar to <a href="https://stackoverflow.com/questions/32001/resettable-java-timer">"Resettable Java Timer"</a> but there are some subtleties I need to explore)</p> <p>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:</p> <pre><code>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(); } } </code></pre> <p>What can I use to implement this? I'm familiar with <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html" rel="nofollow noreferrer">ScheduledExecutorService</a>, and the idea of calling <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Future.html#cancel%28boolean%29" rel="nofollow noreferrer">ScheduledFuture.cancel()</a> and then rescheduling the task <em>seems</em> 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.</p> <p>Also (perhaps more importantly), is there a way to test my implementation / prove that it works properly?</p> <p><strong>edit:</strong> I am particularly concerned about the case where <code>criticalMethod()</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload