Chapter 2: A Simple Two-Thread Example

Chapter 2 - A Simple Two-Thread Example

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Extending the java.lang.Thread Class
An instance of the java.lang.Thread class is associated with each thread running in the JavaVM. These Thread objects serve as the interface for interacting with the underlying operating system thread. Through the methods in this class, threads can be started, stopped , interrupted , named, prioritized, and queried regarding their current state.
  Note There are two ways to create a new class that can have a thread running within it. One way is to extend the Thread class. The other is to extend any class and implement the Runnable interface. For the sake of illustration, extending Thread is the simplest approach and is initially used in this book. Implementing the Runnable interface tends to work much better in the real world; this technique is introduced in Chapter 4, Implementing Runnable Versus Extending Thread.
In this example, the first step towards spawning a new thread is to extend the java.lang.Thread class:
public class TwoThread extends Thread {
    // ...
}
The subclass TwoThread IS-A Thread and consequently inherited the protected and public members from its superclass. TwoThread can be started, stopped, interrupted, named, prioritized, and queried regarding its current state, in addition to all the other behaviors added to the extended class. Figure 2.1 shows the class diagram for TwoThread .
Figure 2.1: The class diagram for TwoThread.
  Note In this book, the notation used for class relationships is closely based on the Unified Modeling Language (UML). Figure 2.2 shows an example of relationships.
  Different terms are used in this book to describe interclass relationships. All the following phrases are true about the relationships depicted in Figure 2.2:
Figure 2.2: A sample class diagram showing generic relationships.
  ClassA is the superclass of ClassB .
  ClassB is a subclass of ClassA .
  ClassB extends ClassA .
  ClassB IS-A ClassA.
  ClassB implements InterfaceF .
  ClassB IS-A InterfaceF .
  ClassB is the superclass of ClassC and ClassD .
  ClassC is a subclass of both ClassB and ClassA .
  ClassC IS-A ClassB, ClassC IS-A ClassA , and ClassC IS-A InterfaceF .
  ClassE contains (at least one reference to) ClassB .
  ClassE HAS-A ClassB reference within it.

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net