Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Threads
In a Java program, each of the simultaneous tasks the computer handles is called a thread, and the overall process is called multithreading. Threading is useful in animation and many other programs.
Threads are a way to organize a program so that it does more than one thing at a time. Each task that must occur simultaneously is placed in its own thread, and this often is accomplished by implementing each task as a separate class.
Threads are represented by the Thread class and the Runnable interface, which are both part of the java.lang package of classes. Because they belong to this package, you don't have to use an import statement to make them available in your programs.
One of the simplest uses of the Thread class is to slow down how fast a program does something.
Slowing Down a Program
The Thread class has a sleep() method that can be called in any program that should stop running for a short period of time. You will often see this technique used in a program that features animation because it prevents images from being displayed faster than the Java interpreter can handle them.
To use the sleep() method, call Thread.sleep() with the number of milliseconds to pause, as in the following statement:
Thread.sleep(5000);
The preceding example will cause the Java interpreter to pause for five seconds before doing anything else. If for some reason the interpreter can't pause that long, an InterruptedException will be thrown by the sleep() method.
For this reason, you must deal with this exception in some manner when using the sleep() method. One way to do this is to place the Thread.sleep() statement inside a try-catch block:
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// wake up early
}
When you want a Java program to handle more than one thing at a time, you must organize the program into threads. Your program can have as many threads as needed, and they can all run simultaneously without affecting each other.
Creating a Thread
A Java class that can be run as a thread is often referred to as a threaded class. Although threads can be used to pause a program's execution for a few seconds, they are often used to the opposite reason: to speed up a program. If you put time-consuming tasks in their own threads, the rest of the program runs more quickly. This often is used to prevent a task from slowing down the responsiveness of a program's graphical user interface.
For example, if you have written a Swing application that loads stock market price data from disk and compiles statistics, the most time-consuming task is to load the data from disk. If threads are not used in the application, the program's interface may respond sluggishly as the data is being loaded. This can be extremely frustrating to a user, especially if the person has decided to close the application and return to more pressing matters, like a game of Windows FreeCell.
There are two ways to place a task in its own thread:
- Put the task in a class that implements the Runnable interface.
- Put the task in a class that is a subclass of Thread.
To support the Runnable interface, the implements keyword is used when the class is created, as in this example:
public class LoadStocks implements Runnable {
// body of the class
}
When a class implements an interface, it indicates that the class supports some kind of additional behavior in addition to its own methods.
Classes that implement the Runnable interface must include the run() method, which has the following structure:
public void run() {
// body of the method
}
The run() method should take care of the task that the thread was created to accomplish. In the stock-analysis example, the run() method could contain statements to load data from disk and compile statistics based on that data.
When a threaded application is run, the statements in its run() method are not executed automatically. Threads can be started and stopped in Java, and a thread won't begin running until you do two things:
- Create an object of the threaded class by calling the Thread constructor.
- Start the thread by calling its start() method.
The Thread constructor takes a single argument—the object that contains the thread's run() method. Often, you will use the this keyword as the argument, which indicates that the current class includes the run() method.
Listing 19.1 contains a Java application that displays a sequence of prime numbers in a text area.
Example 19.1. The Full Text of FindPrimes.java
1: import java.awt.*;
2: import javax.swing.*;
3: import java.awt.event.*;
4:
5: class FindPrimes extends JFrame implements Runnable, ActionListener {
6: Thread go;
7: JLabel howManyLabel = new JLabel("Quantity: ");
8: JTextField howMany = new JTextField("400", 10);
9: JButton display = new JButton("Display primes");
10: JTextArea primes = new JTextArea(8, 40);
11:
12: FindPrimes() {
13: super("Find Prime Numbers");
14: setSize(400, 300);
15: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16: Container content = getContentPane();
17: BorderLayout bord = new BorderLayout();
18: content.setLayout(bord);
19: display.addActionListener(this);
20: JPanel topPanel = new JPanel();
21: topPanel.add(howManyLabel);
22: topPanel.add(howMany);
23: topPanel.add(display);
24: content.add(topPanel, BorderLayout.NORTH);
25: primes.setLineWrap(true);
26: JScrollPane textPane = new JScrollPane(primes);
27: content.add(textPane, BorderLayout.CENTER);
28: setVisible(true);
29: }
30:
31: public void actionPerformed(ActionEvent evt) {
32: display.setEnabled(false);
33: if (go == null) {
34: go = new Thread(this);
35: go.start();
36: }
37: }
38:
39: public void run() {
40: int quantity = Integer.parseInt(howMany.getText());
41: int numPrimes = 0;
42: // candidate: the number that might be prime
43: int candidate = 2;
44: primes.append("First " + quantity + " primes:");
45: while (numPrimes < quantity) {
46: if (isPrime(candidate)) {
47: primes.append(candidate + " ");
48: numPrimes++;
49: }
50: candidate++;
51: }
52: }
53:
54: public static boolean isPrime(int checkNumber) {
55: double root = Math.sqrt(checkNumber);
56: for (int i = 2; i <= root; i++) {
57: if (checkNumber % i == 0)
58: return false;
59: }
60: return true;
61: }
62:
63: public static void main(String[] arguments) {
64: FindPrimes fp = new FindPrimes();
65: }
66: }
The FindPrimes application displays a text field, a Display Primes button, and a text area, as shown in Figure 19.1.
Figure 19.1 Running the FindPrimes application.
Most of the statements in the application are used to create the graphical user interface or display a sequence of prime numbers. The following statements are used to implement threads in this program:
- Line 5: The Runnable interface is supported in the FindPrimes class.
- Line 6: A Thread object is given a name (go), but isn't created yet.
- Lines 33–36: If the go thread has a value of null, which indicates the thread hasn't been created yet, a new Thread object is created under the name go. The thread is started by calling the thread's start() method, which causes the run() method of the FindPrimes class to be executed.
- Lines 39–52: The run() method looks for a sequence of prime numbers beginning with 2, displaying each one in the primes text area component by calling its append() method. The number of primes in the sequence is determined by the value in the howMany text field.
Working with Threads | Next Section

Account Sign In
View your cart