4

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.

Dado Del
  • 75
  • 1
  • 4
  • 1
    The duration of the sleep is calculated to have the game play at a consistent and intended speed (which is why setting sleep to 0 makes the game play too fast). The try and catch are required due to `Thread.sleep` possibly throwing an `InterruptedException`, and do not really have any impact on how the game runs here. – Trevor Freeman Sep 14 '15 at 17:12
  • 1
    Are you asking what the instructions before the try/catch do or what the try/catch is there for? – elaid Sep 14 '15 at 17:18
  • @TrevorFreeman Oh ok I see. Is there a way to still have the game playing at the consistent speed calculated, but without the try/catch operations? – Dado Del Sep 14 '15 at 17:25
  • @TheLostMind could you explain how this is a duplicate of that? That post seems to be only interested in the exception, where this seems more based on implementation. I feel I either missed something or this is mis-labeled. – Sh4d0wsPlyr Sep 14 '15 at 18:03
  • @DadoDel You either need the try / catch operation or the method containing `Thread.sleep` needs to declare that it can throw an `InterruptedException`, since `InterruptedException` is a checked exception and requires explicit handling at all steps. In this case, it is likely best to leave the try catch in place as is. Is there a particular reason you want to remove the exception handling? – Trevor Freeman Sep 14 '15 at 18:50
  • @Sh4d0wsPlyr :-) The OP doesn't understand why he needs a *try-catch* block when using `Thread.sleep()`. – TheLostMind Sep 15 '15 at 04:56

1 Answers1

0

Basically, what Thread.Sleep() does is freeze the thread that the code is running, and in this case, your application's main thread. Think it like your program does:

Do something....

Wait for X time ("freeze" the program execution)

Do something else.

The try/catch operation is not relevant with the delay you see. Basically, try/catch works like a "catcher" in case of something goes wrong on the thread freezing (in this case, what can go wrong is that "someone" (i.e. another thread) will try to interrupt the freeze of the thread).

In that case, with simple words, it will "jump" and execute the code written in the catch block, preventing the crash of your program (think it like a protection layer).

If you remove the try/catch block, then in the abnormal case described above, there will be nothing to prevent the system crash and your program will explode (OK, it won't, don't freak out. :P ).

Community
  • 1
  • 1
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
  • Yes I thought about it, so I tried to delete the try/catch instructions, and the game didn't start anymore, which added doubt in my mind.. – Dado Del Sep 14 '15 at 17:11
  • @DadoDel - `sleep(timeInMilliseconds)` takes time in milliseconds as argument i.e, the time until which the thread has to do nothing. A thread can get *interrupted* when it is asleep :P. So, you need to use, `try-catch` block (or propagate the exception (bad idea)) – TheLostMind Sep 14 '15 at 17:13