0

I have a requirement to run a loop for one minute and after that I will return result accordingly. Inside the loop, I will monitor a value of a variable. Outside the loop, I will return result according to the monitored value of the variable at the last instance of the loop(at the end of one minute).

For this, I have to run the loop for exactly one minute. How can I give the minute checking in the condition of the loop?

Otherwise, Is there any listener to check the variable's value continuously for one minute?

Kalyan
  • 358
  • 1
  • 3
  • 10
  • You can get the time (in milliseconds) with `System.currentTimeMillis();`. You save the return value at the start of the loop and measure the time difference until it's bigger than 60s (60000 ms) – ParkerHalo Jan 12 '16 at 12:27
  • Never use `System.currentTimeMillis()` for timers. Only `System.nanoTicks()`. – Shloim Jan 12 '16 at 12:32
  • Any specific reason behind it? – Kalyan Jan 12 '16 at 12:38
  • @Kaylan: See here http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime ... basically `System.currentTimeMilis()` is not a real timer. It can get distorted by events outside of it (e.g. if you hit a leap second, the user changes the date etc.). – hamena314 Jan 12 '16 at 12:39

0 Answers0