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 ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.