File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Problem with java.util.Timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with java.util.Timer" Watch "Problem with java.util.Timer" New topic

Problem with java.util.Timer

Usman Usmani

Joined: Jun 13, 2005
Posts: 4

I have created a class foo that extends TimerTask and I schedule it
with java.util.Timer.ScheduleAtFixe�dRate() to schedule it. the problem
is that once the timer starts scheduling and i change system date and
time it behaves quite abnormally and starts firing every moment.

Any one knows a way arround this??
Henry Wong

Joined: Sep 28, 2004
Posts: 20538

This is a known problem with fixed rate scheduling. See JavaDoc...

In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

In any case, you should not be playing with the Time if you expect the Timer class to behave correctly... however, the schedule() with fix delay option doesn't have this problem.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Problem with java.util.Timer
jQuery in Action, 3rd edition