Working with Threads

You can start a thread by calling its start() method, which might lead you to believe there’s also a stop() method to bring it to a halt.


Caution

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 resume() and suspend() methods of the class also are deprecated.


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.

The next project you undertake shows how you can stop a thread. The program you are writing rotates through a list of website titles and the addresses used to visit them.

The title of each page and the web address is displayed in a continuous cycle. Users are 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 the NetBeans source editor first and learning about it afterward, you get a chance to enter the full text of the LinkRotator applet at the end of the hour. Before then, each section of the program is described.

The class Declaration

The first thing you need to do in this applet is to use import for classes in the packages java.awt, java.net, java.applet, java.awt.event, and javax.swing.

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

public class LinkRotator extends JApplet
    implements Runnable, ActionListener {

This statement creates the LinkRotator 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 are able to use a run() method in this applet to make a thread begin running. The ActionListener interface enables the applet to respond to mouse clicks.

Setting Up Variables

The first thing to do in LinkRotator is create the variables and objects of the class. Create a six-element array of String objects called pageTitle and a six-element array of URL objects called pageLink:

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

The pageTitle array holds the titles of the six websites that are 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.

The last three things to create are a Color object named butterscotch, an integer variable called current, and a Thread object called runner:

Color butterscotch = new Color(255, 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 find out how to use them during Hour 23, “Creating Java2D Graphics.”

The current variable keeps track of which site is being displayed so you can cycle through the sites. The Thread object runner represents the thread this program runs. You call methods of the runner object when you start, stop, and pause the operation of the applet.

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

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