Starting the Thread

In this applet, the runner thread starts when the applet’s start() method is called and stop when its stop() method is called.

The start() method is called right after the init() method and every time the program is restarted. Here’s the method:

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

This method starts the runner thread if it is not already started.

The statement runner = new Thread(this) creates a new Thread object with one argument—the this keyword. The this keyword refers to the applet itself, designating it as the class that runs within the thread.

The call to runner.start() causes the thread to begin running. When a thread begins, the run() method of that thread is called. Because the runner thread is the applet itself, the run() method of the applet is called.

Running the Thread

The run() method is where the main work of a thread takes place. In the LinkRotator 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(), sets up the value for the thisThread object. The currentThread() method keeps track of the thread that’s currently running.

All statements in this method are part of a while loop that compares the runner object to the thisThread object. Both objects are threads, and as long as they refer to the same object, the while loop continues looping. There’s no statement inside this loop that causes the runner and thisThread objects to have different values, so it loops indefinitely unless something outside of the loop changes one of the Thread objects.

The run() method calls repaint(). 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’s information to display. Changing current causes a different site to be displayed the next time paint() is handled.

This method includes another try-catch block that handles errors. The Thread.sleep(10000) statement causes a thread to pause 10 seconds, 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 as it slept.

Stopping the Thread

The stop() method is called any time the applet is stopped because the applet’s page is exited, which makes it an ideal place to stop a running thread. The stop() method for the LinkRotator 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 stops running.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.92.107