Chapter 4: Implementing Runnable Versus Extending Thread

Chapter 4 - Implementing Runnable Versus Extending Thread

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Interface java.lang.Runnable
Rather than inherit from Thread , a class can implement the interface java.lang.Runnable to allow a thread to be run within it. Runnable specifies that only one method be implemented:
public void run()
This is the same method signature that run() has in Thread . In fact, Thread also implements Runnable ! Note that run() does not take any parameters, does not return anything, and does not declare that it throws any exceptions.
The Runnable interface can be used to get around the lack of multiple inheritance. Figure 4.5 shows SecondCounter extending JComponent and implementing Runnable . SecondCounter
IS-A Component and can be added to containers. SecondCounter also IS-A Runnable and can have a new thread begin execution with its run() method.
Figure 4.5: Getting around the multiple inheritance problem with Runnable.

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