Archived

This topic is now archived and is closed to further replies.

Java run routine question

This topic is 5020 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I decided to learn Java the other day so I read an article here on gamedev on using java to make a simple game applet. The tutorial was wonderful, however it left me in the dark on one thing: Threads, or more specifically the way in which the example uses them. I understand what a thread is, but a section of the sample code remains a mystery to me:
    public void run()
    {
        while(true)
        {
            //update stuff

            repaint();          
        try
        {
            myThread.sleep(1000/30);
        } catch(InterruptedException e)
        {;}
        }
    }
 
The part that I''m confused about is the try/catch portion (and all the stuff it contains). I''m an experienced C++ user but have never used throw/catches before (kinda wierd in college some classes learned it, we didn''t). What does it do? Also, the line myThread.sleep(1000/30); myThread is a thread I''ve created (obviously) but I am unclear on the sleep method. Any help would be appreciated. Mike When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...

Share this post


Link to post
Share on other sites
Thread.sleep pauses the execution of the current thread for the specified number of milliseconds, letting other threads (both java threads you may have created yourself and threads belonging to other applications) get a chance to do some processing. Without that call your app will tend to hog all the cpu time making the entire OS seem laggy. It can also be used to try and run at a fixed time step per frame (like 30fps).

The try...catch is because the Thread.sleep can throw an exception if the sleep is interupted. In this case you don''t really care if this happens, which is why the catch section {;} is empty.

Share this post


Link to post
Share on other sites