Question

I need to execute code after 10 sec, but it need to repeat itself each 10 sec..

I trying doing this but as I saw it start after period of time and dosent stop

final int sec = 1000;
final Timer timer = new Timer();
TimerTask tt = new TimerTask() {

  @Override
  public void run() {
    c.AddLifeToScreen(1);
  }
};

timer.scheduleAtFixedRate(tt,sec*10 ,sec*10 );
Was it helpful?

Solution

You can use threading to implement such functionality. I have give code below.

public class ThreadDemo extends Thread{

    @Override
    public void run(){
        while(true){
            // Your code here
            System.out.println(new Date());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {              
            e.printStackTrace();
            }
        }
    }

    public static void main(String []args){
        ThreadDemo t = new ThreadDemo();
        t.start();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top