CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Timer

  1. #1
    Join Date
    May 2001
    Location
    India
    Posts
    6

    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


  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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.

  3. #3
    Join Date
    Jun 2009
    Posts
    2

    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?

  4. #4
    Join Date
    Apr 2007
    Posts
    425

    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.

  5. #5
    Join Date
    Jun 2009
    Posts
    2

    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 ?

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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
  •  





Click Here to Expand Forum to Full Width

Featured