Java loop for a certain duration

后端 未结 8 2134
花落未央
花落未央 2020-12-13 22:10

Is there a way I can do a for loop for a certain amount of time easily? (without measuring the time ourselves using System.currentTimeMillis() ?)

I.e. I want to do s

相关标签:
8条回答
  • 2020-12-13 22:39

    I don't think there is a way to loop for a certain period of time without checking to see if you've looped for a certain period of time.

    0 讨论(0)
  • 2020-12-13 22:43

    Depending on your use case either of the two sleep() methods in class Thread might fit the bill, but it sounds like you have to fuss with System.currentTimeMillis(), which seems strange.

    Thread thread = Thread.currentThread();
    thread.sleep(10);      // 10 milliseconds
    thread.sleep(12, 345); // 12 milliseconds and 345 nanoseconds.
    
    0 讨论(0)
提交回复
热议问题