O'Reilly logo

Java, A Beginner’s Guide, 5th Edition by Herbert Schildt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Try This 11-1 Extending Thread

ExtendThread.java

Implementing Runnable is one way to create a class that can instantiate thread objects. Extending Thread is the other. In this project, you will see how to extend Thread by creating a program functionally identical to the UseThreadsImproved program.

When a class extends Thread, it must override the run( ) method, which is the entry point for the new thread. It must also call start( ) to begin execution of the new thread. It is possible to override other Thread methods, but doing so is not required.

1. Create a file called ExtendThread.java. Into this file, copy the code from the second threading example (UseThreadsImproved.java).

2. Change the declaration of MyThread so that it extends Thread

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required