I tried to see how a timer's fixed delay method (schedule) works but looks like i failed.
This is my code :
public class Timer_Test {
static Timer _a_timer;
static int num_o_proc = 0;
static int timer_call = 0;
static double prog_begin_time;
public static void main(String[] args) {
prog_begin_time = System.currentTimeMillis();
_a_timer = new Timer();
_a_timer.schedule(new TimerTask() {
@Override
public void run() {
timer_call++;
System.out.println(timer_call + " timer start at " + (System.currentTimeMillis() - prog_begin_time));
process();
System.out.println(timer_call + " timer end at " + (System.currentTimeMillis() - prog_begin_time));
if (timer_call >= 5) {
System.exit(0);
}
}
}, 1000, 2000);
}
public static void process() {
num_o_proc++;
int local_num_o_proc = num_o_proc;
System.out.println(local_num_o_proc + " process start at " + (System.currentTimeMillis() - prog_begin_time));
double _a_ = 0;
for(int x=0; x<Integer.MAX_VALUE/2; x++) {
_a_++;
}
System.out.println(local_num_o_proc + " process end at " + (System.currentTimeMillis() - prog_begin_time));
}
}
This is what I get :
1 timer start at 1000.0
1 process start at 1000.0
1 process end at 2109.0
1 timer end at 2109.0
2 timer start at 3000.0
2 process start at 3000.0
2 process end at 4109.0
2 timer end at 4109.0
3 timer start at 5000.0
3 process start at 5000.0
3 process end at 6109.0 .....
Shouldn't timer 2 start at 4109 (instead of 3000) since the first timer task ended at 2109 (2109 + 2000)? I tried using 'scheduleAtFixedRate' and it gave me exact same result. Did I do something wrong? Or is there some concept I fail to understand?