In Java you can schedule task using Timer and Timer task class for example demo
import java.util.Timer;
import java.util.TimerTask;
public class MyTask extends TimerTask{
Timer timer;
int count=0;
public MyTask(){
}
public MyTask(Timer timer){
this.timer=timer;
}
public void toDo(){
System.out.println("count-> "+(count++));
}
@Override
public void run() {
toDo();
if(count>10){//this is the condition when you want to stop the task.
timer.cancel();
}
}
}
for this timer you can run this as follows
public static void main(String[] args){
Timer timer=new Timer();
MyTask myTask=new MyTask(timer);
int firstSart=1000;// it means after 1 second.
int period=1000*2;//after which the task repeat;
timer.schedule(myTask,firstSart,period);//the time specified in millisecond.
}