Home E-Books Special Effects and Game Development in Java The theory behind program threads 

Special Effects and Game Development in Java(TM) - The theory behind program threads 

by Anibal Wainstein

3.0 The theory behind program threads 

Multiple program threads were introduced to PC programming with the release of Windows 95. Operating systems such as UNIX have had this for a long time. A thread works like a "little worker" working with the program code. If you have several programs running in your system, then there is at least one thread for each program. Some programs may have several threads, for instance a web browser that supports Java. To be able to have animated Java applets we are forced to use two threads. First there is the program or main thread, which is the Java Virtual Machines own thread. This is used for the so-called event system, which handles user input such as mouse pointer movements and keyboard events. In the last chapter we had only worked with the main thread. The problem with animations is that they often requires that you have a thread that is constantly working with the animation code. We cannot use the main thread because then the applet's other functions will stop working, the applet hangs. A program that hangs, usually does that because the main thread gets stuck in an program loop and cannot get free, or that it collides with another thread and stops. Assume that two web designers are working with a homepage. If they do not decide who is going to do what, then they will probably end up writing each other work. This leads to aggression, the cooperation breaks down and with that also the project. In the same way you must be careful when using threads.

3.0.1 An applet's start() and stop() methods (The Runnable interface) 

Applets have no natural support for threads. You add thread support by adding the Runnable interface :

import java.applet.*;  
import java.awt.*;
public class threadtest extends Applet implements Runnable { } 

In our empty threadtest applet we have added "implements Runnable". With the declaration "implements" you can add several new functions to a class. It is used to increase the class' abilities. "Runnable" will give access to three new methods: start(), stop() and run(). In an applet that supports threads the methods init() and paint() are first called by the Java Virtual Machine's main thread, the third method called by this thread is the start() method.
In the start() method you can initialize and start your animation thread.

public Thread programthread = null
public void start() {
    if (animationthread == null
         animationthread = new Thread(this,"animationthread");
    } }
The variable "animationthread" is a global variable within the applet and you must declare it yourself. What the start() method does first is that it checks that the thread is not already created (sometimes some web browsers can execute the start() method several times). The next step is to create the thread itself. It demands a reference to your applet (this) and a name that could be anything (we call it "animationthread" for the sake of simplicity). After this you start the thread with its own start() method.
When the homepage visitor changes the page or shut down the web browser, then the stop() methods is immediately called by the applet's main thread: 
public void stop() 
    if ((animationthread != null) && animationthread.isAlive())
    animationthread = null; }
For security reasons you should make sure that the thread is created and that it is running with the method isAlive(). If it is then the thread is stopped with it's own stop() method and we set the variable "animationthread" to the value null.

You will probably always just cut and paste these two methods into your programs in the future. They are general methods that all the special effects or game applets have.



Next Page >>