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 );
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.