Home E-Books Special Effects and Game Development in Java how to put a thread to sleep 

Special Effects and Game Development in Java(TM) - Exceptions, static classes and how to put a thread to sleep 

by Anibal Wainstein

3.0.3 Exceptions, static classes and how to put a thread to sleep 

You may have notice that the applet in the last section was very processor intensive. Maybe it is not necessary to update "Hello Sweden!" every millisecond, it is enough doing that once per second or less. We must now put a delay in the run() method's loop. With the method sleep(), which exist in the Thread class, we can get the thread to "sleep" a number of milliseconds. This method is a so-called static method. A method of this type may be called without having to create an instance of the class (in other words, without having to create an object, based on that class). So you can simply write:


Now the thread will sleep for exactly 1000 milliseconds (which is exactly one second). Static classes can be useful sometimes if you happen to have a very useful function in a class that you use a lot. Then it can be a good idea top make the function static and to be able to save some memory by not having to allocate memory for a new object. The memory that is required for a static method is allocated first when the Java VM encounters the class where the static method is. All the future objects created and based on that class will always use the same memory cell where the static method exist.

There is something that you must consider. Static methods cannot work with non-static variables in the same class. However, this is not true if the variables are also static. Why, do you ask? Because you have not allocated memory for these variables (created a new object). They simply do not exist when the static function is called! The effect of using static variables is that if you have several objects based on the same class, then they will all share the same variable. That means that if a static variable is manipulated in one object then the content of the other statical variables in the other objects are also changed.

Now you probably think that you can use this method freely and put it in your loop. No, I am sorry, we are not quite there yet. The problem with the sleep() method is that it throws an exception that you must catch. Exceptions are used in Java by methods to tell to the class calling them that something went wrong. In this case another thread may interrupt the sleeping process. You must know what happened and therefore the function throws an exception. This is of more bother than use, because if you do not catch the exception, then the applet is stopped, or as in some cases, the compiler will simply not accept it. First, you place the method in a try-loop and then you catch its exception in a catch statement: 

    Thread.sleep(1000);  }  catch (InterruptedException e)  {      System.out.println("Something interrupted the sleep() method") } 

The catch loop requires an argument which must be an exception class. The interruption class is named InterruptedException and it is thrown by sleep(). There are many types of exceptions, and all are subclasses to the Exception class. Other well known exceptions are NullPointerException (appears when you have tried to use a null reference), ArrayIndexOutOfBoundsException (appears when you have specified a negative or too high index on an array) and IOException (an error occurred when reading a file). 
To make our "effect" more interesting, we remake the run() method in the last section:

public void run()
    while (true)     {         showStatus("Long");         try {Thread.sleep(1000);}          catch(InterruptedException e) {}         showStatus("Live");         try {Thread.sleep(1000);}         catch(InterruptedException e) {}         showStatus("Java!");         try {Thread.sleep(1000);}          catch(InterruptedException e) {}     } }
In order to make the code more easy to read, I have written the pause code on two lines and removed the println() message, because it is not very interesting in this case to know if another thread interrupts the waiting process. Can you guess what will happen now? First, the message "Long" is written to the status window, then the thread will wait one second and then "Live" will be written, after another second, the string "Java!" is written. One second later, the program will start over again.

Click here and you will see. 

Now you will also notice that your computer is not working so hard compared with the other example.



Next Page >>