Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have already canceled one timer, you can't re-start it, you'll have to create a new one. </p> <p>See this <a href="https://stackoverflow.com/questions/2096578/java-replacement-for-infinite-loops/2097100#2097100">answer</a>, it contains a video and the source code how I did something similar.</p> <p>Basically there are two method: <code>pause</code> and <code>resume</code></p> <p>In pause:</p> <pre><code>public void pause() { this.timer.cancel(); } </code></pre> <p>In resume:</p> <pre><code>public void resume() { this.timer = new Timer(); this.timer.schedule( aTask, 0, 1000 ); } </code></pre> <p>That makes the perception of pause/resume.</p> <p>If your timers perform different actions based on the state of the application you may consider use the <a href="http://en.wikipedia.org/wiki/State_pattern" rel="nofollow noreferrer">StatePattern</a></p> <p>Fist define a abstract state:</p> <pre><code>abstract class TaskState { public void run(); public TaskState next(); } </code></pre> <p>And provide as many states as you like. The key is that one state leads you to another. </p> <pre><code>class InitialState extends TaskState { public void run() { System.out.println( "starting..."); } public TaskState next() { return new FinalState(); } } class FinalState extends TaskState { public void run() { System.out.println("Finishing..."); } public TaskState next(){ return new InitialState(); } } </code></pre> <p>And then you change the state in your timer.</p> <pre><code>Timer timer = new Timer(); TaskState state = new InitialState(); timer.schedule( new TimerTask() { public void run() { this.state.run(); if( shouldChangeState() ) { this.state = this.state.next(); } } }, 0, 1000 ); </code></pre> <p>Finally, if what you need is to perform the same thing, but at different rates, you may consider using the <a href="https://timingframework.dev.java.net/" rel="nofollow noreferrer">TimingFramework</a>. It is a bit more complex but let's you do cool animations, by allowing the painting of certain component take place at different rates ( instead of being linear ) </p>
    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