Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The error is because when you call <code>cancel()</code> the <code>Timer</code> is 'dead' and you can't do anything else with it.</p> <p>From the <a href="http://download-llnw.oracle.com/javase/6/docs/api/java/util/Timer.html#cancel()" rel="noreferrer">API</a>:</p> <blockquote> <p>Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it. </p> </blockquote> <p>You have a few options:</p> <ol> <li>Get rid of the static <code>Timer</code> and instead hold a separate instance of <code>Timer</code> within each object. That way when the object is destroyed, you can also destroy the <code>Timer</code> without affecting the other <code>TimerTask</code>s. This will mean one timer thread per object.</li> <li>Keep the static <code>Timer</code> but also hold in memory (e.g. in an <code>ArrayList&lt;TimerTask&gt;</code>) a list of all the tasks within it. Then when an object is destroyed, you re-create the static <code>Timer</code> object using the in-memory list of tasks (minus the one corresponding to the object you destoryed). The consequence of this is that the execution of the remaining <code>TimerTask</code>s may need some finessing (particularly if you don't want them all to 'bunch up').</li> <li>Extend or write your own <code>Timer</code>-like class to allow for the removal of <code>TimerTask</code>s. (Someone may have already done this, I don't know)</li> <li>Use a <a href="http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html" rel="noreferrer">ScheduledThreadPoolExecutor</a> which allows you to remove tasks.</li> </ol>
    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. 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