I found these instructions on the web, inside a code of a game. However, I don't understand how it works.
start = System.nanoTime();
elapsed = System.nanoTime() - start;
wait = (100/60) - (elapsed / 1000000);
if(wait < 0)
{
wait = 4;
}
try
{
Thread.sleep(wait);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
I know that Thread.sleep(wait)
makes the thread waiting the amount wait
in ms before starting. But in this case, why bother with all these instructions? I tried putting Thread.sleep(0)
, and the speed of the game was like 20times faster (all objects moving way too fast).
How does these instructions work?
Thanks in advance.