Working with Threads

You can start a thread by calling its start() method, which may lead you to believe that there's also a stop() method to bring it to a halt. Although Java includes a stop() method in the THRead class, it has been deprecated. In Java, a deprecated element is a class, interface, method, or variable that has been replaced with something that works better. Anything that has been deprecated can still be used, but may be removed from the language entirely in a future version.

Watch Out!

It's a good idea to heed this deprecation warning. Oracle has deprecated the stop() method because it can cause problems for other threads running in the Java interpreter.


The next project you undertake will show how a thread can be stopped. The remainder of this hour covers an extended project describing the design of a threaded applet. The program you'll be writing will rotate through a list of website titles and the addresses used to visit them. The title of each page and the web address will be displayed in a continuous cycle. Users will be able to visit the currently displayed site by clicking a button on the applet window. This program operates over a period of time, displaying information about each website in sequence. Because of this time element, threads are the best way to control the program. Instead of entering this program into your word processor first and learning about it afterward, you'll get a chance to enter the full text of the Revolve applet at the end of the hour. Before then, each section of the program will be described.

The class Declaration

The first thing you need to do in this applet is to use import to make some classes available. The java.awt group of classes is needed because you'll be using one of them, Graphics2D, to display text onscreen. The java.net group will be used when you work with the web addresses, and the java.applet group is needed when you tell the browser to load a new page. Finally, the java.awt.event group is needed to respond to mouse clicks so a user can visit one of the addresses shown, and the java.awt.swing package is needed because it is used to create the applet window and other user interface elements. Use the following import statements:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;


After you have used import to make some classes available, you're ready to begin the applet with the following statement:

public class Revolve extends JApplet
 implements Runnable, ActionListener {


This statement creates the Revolve class as a subclass of the JApplet class. It also indicates that two interfaces are supported by this class—Runnable and ActionListener. By implementing the Runnable class, you will be able to use a run() method in this applet to make a thread begin running. The ActionListener interface enables the applet to respond to actions the user takes with the mouse. Implementing it enables the actionPerformed() method to be called when a mouse button is clicked.

Setting Up Variables

The first thing to do in the Revolve class is create the variables and objects needed throughout the program. Create two arrays with six elements—an array of String objects called pageTitle and an array of URL objects called pageLink:

String[] pageTitle = new String[6];
URL[] pageLink = new URL[6];


The pageTitle array will store the titles of the six websites that will be displayed. The URL class of objects stores the value of a website address. URL has all the behavior and attributes needed to keep track of a web address and use it to load the page with a web browser. Both of these arrays are set up without any values at this point, so you'll have to provide them later. The last three things to be created are a Color object named butterscotch, an integer variable called current and a Thread object called runner:

Color butterscotch = new Color(204, 204, 158);
int current = 0;
Thread runner;


Color objects represent colors you can use on fonts, user interface components, and other visual aspects of Swing. You'll find out how to use them during Hour 22, "Using Fonts and Color." The current variable will be used to keep track of which site is being displayed so you can cycle through the sites. The Thread object runner represents the only thread this program runs. You will call methods of the runner object when you start, stop, and pause the operation of the applet.

      
Comments