Strange try / catch commands

I found these instructions on the Internet, inside the game code. However, I do not understand how this 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 what a Thread.sleep(wait)thread is doing, expecting a count waitin milliseconds before starting. But in this case, why bother with all these instructions? I tried to bet Thread.sleep(0), and the game speed was about 20 times faster (all objects moved too fast).

How do these instructions work?

Thanks in advance.

+4
source share
1 answer

Basically, what it Thread.Sleep()does is freezing the thread in which the code works, in which case the main thread of the application. Think about how your program does it:

Do something....

X ( "" )

- .

try/catch , . , try/catch "" , - ( , , , "-" (.. ) ).

"" , catch, (, ).

try/catch, , , , , (, , .: P).

0

Source: https://habr.com/ru/post/1607308/


All Articles