Starting the Thread

One of the objects created for this program is a THRead object called runner. For a thread to get started, a place is needed where the thread is given a value and told to begin running. In this applet, the runner tHRead will start whenever the start() method is handled and stop whenever stop() is handled. The start() method of an applet is handled at two different times: right after the init() method and every time the program is restarted after being stopped. An applet is stopped any time a user switches from the applet page to another web page. It starts again when a user returns to the original page. The following is the start() method of the Revolve applet:

public void start() {
 if (runner == null) {
 runner = new Thread(this);
 runner.start();
 }
}


This method does only one thing: If the runner thread is not already started, it creates a new runner thread and starts it. The runner object equals null when it has not been started yet, so you can test for this condition with the if statement. The statement runner = new Thread(this); creates a new THRead object with one argument—the this statement. Using this makes the applet itself the program that will run in the runner thread. The runner.start(); statement causes the thread to begin running. When a thread begins, the run() method of that thread is handled. Because the runner thread is the applet itself, the run() method of the applet is handled.

Running the Thread

The run() method is where the main work of a thread takes place. It is comparable to the main() block statement of a Java app. In the Revolve applet, the following represents the run() method:

public void run() {
 Thread thisThread = Thread.currentThread();
 while (runner == thisThread) {
 current++;
 if (current > 5) {
 current = 0;
 }
 repaint();
 try {
 Thread.sleep(10000);
 } catch (InterruptedException e) {
 // do nothing
 }
 }
}


The first thing that takes place in the run() method is the creation of a THRead object called thisThread. A class method of the THRead class, currentThread(), is used to set up the value for the thisThread object. The currentThread() method keeps track of the thread that's currently running. All of the statements in this method are part of a while loop that compares the runner object to the thisThread object. Both of these objects are threads, and as long as they have the same value, the while loop will continue looping. There's no statement inside this loop that causes the runner and thisThread objects to have different values, so it will loop indefinitely unless something outside of the loop changes one of the THRead objects. The run() method first uses the repaint(); statement to cause the paint() method to be handled. Next, the value of the current variable increases by one, and if current exceeds 5, it is set to 0 again. The current variable is used in the paint() method to determine which website information to display. Changing current causes a different site to be displayed the next time paint() is handled. This method includes another try-catch statement that handles an error that might occur. The Thread.sleep(10000); statement causes a thread to pause for 10,000 milliseconds. This statement causes the thread to wait long enough for users to read the name of the website and its address. The catch statement takes care of any InterruptedException errors that might occur while the Thread.sleep() statement is being handled. These errors would occur if something interrupted the thread while it was trying to sleep().

Stopping the Thread

The stop() method is handled any time the applet is stopped because the applet's page is exited, and it is the best place to stop the running thread. The stop() method for the Revolve applet contains the following statements:

public void stop() {
 if (runner != null) {
 runner = null;
 }
}


The if statement tests whether the runner object is equal to null. If it is, there isn't an active thread that needs to be stopped. Otherwise, the statement sets runner equal to null. Setting the runner object to a null value causes it to have a different value than the thisThread object. When this happens, the while loop inside the run() method will stop running.

      
Comments