Results 1 to 12 of 12
  1. #1
    Nuluvius is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Arrow Best way to implement a timer

    Hey,

    I have a quick question, what is the best method for implementing a timer thread. Say you want to record the amount of elapsed time betwean two actions - for instance .. maybe:

    user clicks button - timer starts.
    user clicks button - timer stops.

    getting system current time stored then deducting it from system current time on the second action is a rather inificient way it seemed to me.. Is there a more efficient and eligant method for going about this?

    Thanks in advance, I am ever apprechiative.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post simple

    Bascially, that's the way to do it. I often worry about allocation sizes and so on, but that worry is misplaced effort. The machines today can pull a java stack and compilers can reduce just about whatever one can code to effieicent runtimes using what is called code path analysis. In any event, your primary task is deducting the logic:
    Java Code:
    boolean buttonDown = false;
    void onButtonPush(){
       if(buttonDown)
       {
           [B]buttonDown=false:[/B]
           interval = getSystemTime() - start time;
       }
       else
       {
          buttonDown=true;
          start time = get ....
    not hard to do, just takes several trials at coding it but in any event, today's machines are remarkably powerful. Just write code you can understand.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Nuluvius is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Default

    Cheers for that Nicholas, I did have a suspicen as such but thought it prudent to check with an expert. Thanks for the help!

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Okay, it always helps in front of beginners to be called an expert. I am not formally trained, just invested a lot of thought into things like this and read a pile of books.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    How would you code the method, getSystemTime()?

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Long value = new Long(System.currentTimeMillis());
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Why use a Long where a long will suffice?
    Java Code:
    long start = System.currentTimeMillis();
    :
    :
    long duration = System.currentTimeMillis - start;
    db

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Well it will, but compiler do autoboxing blindside so later we have to take it from poster why null pointer exception, so I just do it this way in my code and I was digging through previous posts and did it this way because it has .toString() which is usually how somebody with twelve posts does it. Later we have to explain value of or whatever it is for this one, I cannot remember them all but I refuse to run on small, tight code.......
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    No need for valueOf, no need for toString. Just concat the long to an empty String.
    Java Code:
    String strDuration = "" + duration;
    db

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm...

    Agreed... that would work, but it's not the correct way to do it. It's more like a quick & dirty way to do it. Using the proper methods is probably the best way to go.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post What the capabilities of the machine actually are.

    Code correctness here would be to have poster direct attention to doing some get-going work on what the capabilities of the machine actually are. I do this all the time, I am stiil not out of the mud on the matter.

    There was a day when 65k was a luxury, ram was loaded with a boot loader that began with an 80 colum card with a paticular character in the first colum or so....in that domain, reading the clock would be .... what's a better word for non-tivial, significant?

    Obscure tricks to gain a few bits here and there were part and parcel of a coder's skills, 4k would be a common programming space for both machine processor instructions and variable .....

    Today, a keychain calculator has processor power a business then may have only wished for, useable equipment gets wasted on 16-bit ( or even 8-bit possibly ) instructions that, at best, are designed intentionally to choke the machine down so that it is necessary to download and download ad infinitum, the capabilities of the machine are in general beyond the grasp of those who use the machine. I had an old-timer tell me that the data channel of one high school shop grade optical device is beyond human grasp.

    Reading the clock, i dunno one assembler op? No more that ten or twenty at most - which at even a 3-mhz trainer board is vanishingly small. Todays gigabit rates, with terabit either on the board or actually in production, especially combined with 64 to 80 or whatever it is processors already running on one mount on a bench in the shop,...whether poster wants to do Numeric.getValue() or long time = System.readClock() would be either a personal choice or that of posters instructor or employer.

    Getting in scope what the capabilites of the machine are would be Code Correctness here.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  12. #12
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Good point...

    If the OP is looking for the fastest way (less machine cycles), then Darryl's way to do it is probably the most appropiate. I was looking at it more like the correct/appropiate/understandable way of coding it.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. EJB Timer
    By mrjunsy in forum Advanced Java
    Replies: 0
    Last Post: 08-22-2008, 05:09 PM
  2. EJB Timer
    By mrjunsy in forum New To Java
    Replies: 0
    Last Post: 08-04-2008, 07:47 PM
  3. Replies: 0
    Last Post: 04-04-2008, 03:46 PM
  4. CountDown timer
    By Seema Sharma in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 05:26 PM
  5. Page Timer
    By deeadeed in forum New To Java
    Replies: 0
    Last Post: 12-05-2007, 09:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •