O'Reilly logo

Special Edition Using Java 2 Standard Edition by Geoff Friesen, Chuck Cavaness, Brian Keeton

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

Implementing Runnable

The second way to cause classes to behave as threads is to have those classes implement the java.lang.Runnable interface. A second method is available because extending the Thread class will not work in all cases. This is because Java does not allow for a class to be a subclass of more than one direct parent class. Other languages such as C++ do allow this, but Java does not. For example, the SimpleThread class shown previously could not extend Thread and Date classes simultaneously.

If you need to create a new class that acts as a thread and this new class already extends a different class, the alternative is to have the new class implement the Java Runnable interface instead. The Runnable interface defines a single interface ...

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