Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Thread.sleep Exception
    text
    copied!<p>I've made a class that controls the speed at which my program runs, kind of like a vertical sync, and it passes some necessary information to the program at each frame. I have the entire thing working, but I've tried using the more accurate Thread.sleep(long millis, int nanos), something I'm fairly inexperienced using. Based upon the descriptions I've seen, it simply adds the milliseconds provided to the nanoseconds, then pauses the thread. But, about 90% of the frames throw an odd exception, which I really cannot decipher.</p> <pre><code>java.lang.IllegalArgumentException: nanosecond timeout value out of range </code></pre> <p>The following is most of the code I used, that interacts in any way with the variable I use to delay the thread.</p> <pre><code>long startTime = System.nanoTime(); while (! this.stop) { try { // Run Frame long endTime, deltaTime, deltaRemainder; endTime = System.nanoTime(); deltaTime = endTime - startTime; deltaRemainder = this.rate - (deltaTime % this.rate); System.out.println("Frame Completed with " + (double)(deltaRemainder * .000000001) + " seconds left!"); if (deltaRemainder &gt; 0) Thread.sleep(0, (int)(deltaRemainder)); startTime = System.nanoTime(); } catch (Exception e) { e.printStackTrace(); } } </code></pre> <p>rate is the variable which is equal to the length of a frame in nanoseconds, stop is really unimportant, and apparent. start/endTime are the times at the start and end of a frame. deltaTime is the length of time the frame took to finish. And finally, deltaRemainder is the amount of extra time the frame should have taken to finish(if it takes longer than it should, it jumps up to end at the next possible frame).</p> <p>Could anyone explain why this exception is thrown? I would really prefer to use the accuracy this function provides.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload