Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From <a href="http://docs.oracle.com/javase/6/docs/api/java/util/TimerTask.html" rel="nofollow noreferrer"><code>TimerTask.cancel()</code></a>:</p> <blockquote> <p>Note that calling this method from within the run method of a repeating timer task absolutely guarantees that the timer task will not run again.</p> </blockquote> <p>So once cancelled, it won't ever run again. You'd be better off instead using the more modern <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html" rel="nofollow noreferrer"><code>ScheduledExecutorService</code></a> (from Java 5+).</p> <p><strong>Edit:</strong> The basic construct is:</p> <pre><code>ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); exec.scheduleAtFixedRate(runnable, 0, 1000, TimeUnit.MILLISECONDS); </code></pre> <p>but looking into it there's no way of cancelling that task once its started without shutting down the service, which is a bit odd.</p> <p><code>TimerTask</code> might be easier in this case but you'll need to create a new instance when you start one up. It can't be reused.</p> <p>Alternatively you could encapsulate each task as a separate transient service:</p> <pre><code>final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); Runnable task1 = new Runnable() { public void run() { a++; if (a == 3) { exec.shutdown(); exec = Executors.newSingleThreadScheduledExecutor(); exec.scheduleAtFixedRate(task2, 0, 1000, TimeUnit.MILLISECONDS) } } }; exec.scheduleAtFixedRate(task1, 0, 1000, TimeUnit.MILLISECONDS); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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