Problem with java.util.Timer

B

brownk13

We are having a strange problem using the Timer class. I was wondering
if anybody else has had this problem or at least know what may be
happening.

The code is scheduling an automatic backup to occur. When the user
sets up the schedule then can choose a day for the event to run, and a
time of the day. This is working pretty good, but there is one
scenario that causes the Timer to be run over and over.

If the user selects an hour of the day later than the current day, and
an earlier day of the week it will continually run. For example, if
today is Thursday at 10:00a.m. and the user sets the backup to run on
Monday at 7:00p.m. it will get into this state.

When the Timer runs the event, the last thing that the triggered event
does is call this method...

private void setupNextTimer() {
Timer timer = new Timer(true);
timer.schedule(new BackupTimerTask(),calculateNextRunTime());
}

The calculateNextRunTime() method returns a java.util.Date representing
the next time the event should run. I debugged in to see if the next
run time was messed up. The Date that it was returning was for Monday
@ 18:00 (or something like that), I was testing this on Thursday @
10:00. (debugging in I was able to see the long value for the new date
is 1164668400183.

As soon as the Timer schedules the task it immediatly runs
BackupTimerTask, which then schedules the next time, and as soon as it
is scheduled BackupTimerTask is run again, over and over.

Just want to see if anybody has any ideas on what may be causing the
problem

Thanks
 
W

wesley.hall

We are having a strange problem using the Timer class. I was wondering
if anybody else has had this problem or at least know what may be
happening.

The code is scheduling an automatic backup to occur. When the user
sets up the schedule then can choose a day for the event to run, and a
time of the day. This is working pretty good, but there is one
scenario that causes the Timer to be run over and over.

If the user selects an hour of the day later than the current day, and
an earlier day of the week it will continually run. For example, if
today is Thursday at 10:00a.m. and the user sets the backup to run on
Monday at 7:00p.m. it will get into this state.

When the Timer runs the event, the last thing that the triggered event
does is call this method...

private void setupNextTimer() {
Timer timer = new Timer(true);
timer.schedule(new BackupTimerTask(),calculateNextRunTime());
}

The calculateNextRunTime() method returns a java.util.Date representing
the next time the event should run. I debugged in to see if the next
run time was messed up. The Date that it was returning was for Monday
@ 18:00 (or something like that), I was testing this on Thursday @
10:00. (debugging in I was able to see the long value for the new date
is 1164668400183.

As soon as the Timer schedules the task it immediatly runs
BackupTimerTask, which then schedules the next time, and as soon as it
is scheduled BackupTimerTask is run again, over and over.

Just want to see if anybody has any ideas on what may be causing the
problem


It is almost certainly a bug in the calculateNextRunTime() method where
the date returned is in the past.

Change your code to this...

private void setupNextTimer() {
Date scheduledDate = calculateNextRunTime();
System.out.println("Scheduling for: " + scheduledDate);
Timer timer = new Timer(true);
timer.schedule(new BackupTimerTask(), scheduledDate);
}

And look at the console output very carefully. I bet it prints a date
in the past.
 
B

brownk13

Thanks,

You nailed it. It's funny printing out the date made it clear that it
was an earlier date. Looking at the date in the debugger for some
reason was much easier to miss that it was earlier. Sometimes you just
get too focused when working on problems. :)
 
W

Wesley Hall

Thanks,

You nailed it. It's funny printing out the date made it clear that it
was an earlier date. Looking at the date in the debugger for some
reason was much easier to miss that it was earlier. Sometimes you just
get too focused when working on problems. :)

I know the feeling! :)

Glad I could help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top