• Post Reply Bookmark Topic Watch Topic
  • New Topic

is this a good stopwatch code?  RSS feed

 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a puzzle App in which there's a timer that starts with the puzzle and stops when the user solves it, it tells the time it takes to finish the puzzle. so far for my timer, I built 3 nested for loops; a loop for Hours that runs 24 times, a loop for minutes that runs 60 times and finally a loop for seconds that runs for 60 times as well, and at the heart of it I used Thread.sleep(1000) and the change is shown through a TextView(this is for android)
I would like to get some feedback on the whole thing. btw, I added code to stop and resume the stopwatch.

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure about the Android but you dont have to use 3 loops. It can simply as:




Also in your code you don't have to compare true. So instead of


This will be fine


 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you just want to know the time it took to solve the puzzle, or are you displaying the stop watch and updating it every second?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote: . . . you don't have to compare true. So instead of . . .
Spot on

In fact you can get nasty errors if you use == true or == false and write = instead of == by mistake.
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:Do you just want to know the time it took to solve the puzzle, or are you displaying the stop watch and updating it every second?

I want it to display and update the stopwatch every second, or perhaps centisecond
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I ask the compiler for time? and does it return type in seconds?
but after a second try, I managed to reduce my loops to just one for loop and that is for Centi-seconds, and using both / and % operator I converted it to String format of Minutes:seconds:centisconds

is this better?
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Tushar Goel wrote: . . . you don't have to compare true. So instead of . . .
Spot on

In fact you can get nasty errors if you use == true or == false and write = instead of == by mistake.

I didn't know I could do that, thanks, I'll keep it in mind from now on
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than using Thread.sleep(), you may want to look into ScheduledExecutors. Look at https://developer.android.com/reference/java/util/concurrent/ScheduledThreadPoolExecutor.html.

To get a time value you can use System.currentTimeMillis(). This will return a time value in milliseconds. You can get a reference value at the start and then use subsequent calls to that function to work out the time difference.

I think one problem with your implementation is that it assumes that Thread.sleep sleeps for exactly the amount of time you asked it to. In reality it can sleep for longer than you asked it to. It probably doesn't matter for a stop watch, but if you were doing something where accurate timing at high resolution was important then you would find your time calculations drifting.

One other thing is that you should not catch InterruptedException and then continue, that will infuriate developers that use your class. The InterruptedException is a message from some code that wants your thread to stop, and by ignoring the message you may cause your program to fail to terminate.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!