-
February 11th, 2002, 04:57 AM
#1
Timer
When I am scheduling Timer Task using java.util.Timer class , exceution is happening properly. But if I modify Computer's System Time then Timer is not responding properly.
Suggest some way to overcome this.
Regards,
Sandip
-
February 11th, 2002, 09:05 AM
#2
Re: Timer
The java.util.Timer class uses the system time to schedule events. If you change the system time, it will affect the timers. The best way to not affect time scheduling is not to mess with the system time...
Dave
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
June 29th, 2009, 09:33 PM
#3
Re: Timer
what if i cant stop the user from changing the system time ?
i am using thread.sleep(1000) to cause a method to run every 1 sec.. but after reading online.. it seems to be a lousy design as it depends on the system cpu speed.
is there any other ways to execute a method every 1 sec and able to work while changing system time?
-
June 29th, 2009, 11:02 PM
#4
Re: Timer
http://java.sun.com/j2se/1.5.0/docs/...Executors.html
In general, you should obtain a Thread pool from the system. Scheduled Thread Pools will allow you to invoke a task at scheduled intervals. Anything that is a Runnable qualifies.
-
June 30th, 2009, 12:40 AM
#5
Re: Timer
I have tried something like:
private ScheduledExecutorService scheduler
private ScheduledFuture task
public classname(){
scheduler = Executors.newSingleThreadScheduledExecutor();
task = scheduler.scheduleAtFixedRate(methodToRun,1,1,TimeUnit.SECONDS)
}
This works but if i changed the system time for multiple times, it will just stop my program...
Did i use ScheduledExecutorService or thread right ?
-
June 30th, 2009, 07:18 AM
#6
Re: Timer
i am using thread.sleep(1000) to cause a method to run every 1 sec.. but after reading online.. it seems to be a lousy design as it depends on the system cpu speed.
No this isn't a lousy design, your use of it is wrong. The line of code you posted causes the thread to sleep for 1 second. If other threads are running when the sleep time has finished then the sleeping thread may or may not immediately start running again depending on a number of factors and even when it does run the code in the loop will take an amount of time to execute so your loop will not execute every second. The only guarantee is it is unlikely to execute quicker than every second (If the sleep is interrupted, depending on your code, it may run after less than 1 second) .
The timer uses the system clock to work out the delay before the next timer event. If you change the system clock it is going to effect the delay. If you want to be able to change the system clock then you need to work out how to time the loop without relying on the system clock.
How critical is the time between executions?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|