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