Why two ways (implementing Runnable,extending Thread) to create a 'thread' in java?
1 posts in topic
Flat View  Flat View

Posted By:   Kalanithi_Srinivasan
Posted On:   Friday, July 11, 2003 04:28 AM

We all know that Sun has provided two ways to create
a thread in Java class,
* By implementing Runnable Interface,
* By extending a Thread class
Why not SUN could have stopped by giving the first option alone, ie., by implementing the Runnable interface only.
But it is clear that the second way, by extending Thread class will not be helpful if our class already extends one more class(lack of multiple Inheritance in java), in such case implementing Runnable interface would help us. But I feel the way of implementing Runnable Interface to achieve 'threading' would be idle in all situations and am wondering why the second option?

Re: Why two ways (implementing Runnable,extending Thread) to create a 'thread' in java?

Posted By:   Anonymous  
Posted On:   Monday, July 14, 2003 02:50 AM

Hi Srinivasan,

Option 1:
Runnable Interface

Runnable interface has only one method declared in it. So if you choose this option, just make your thread class implement this interface and provide the implementation for the run method.

Thread runnableT = new Thread(myClassEWithRunnable);

So, here 'runanbleT' will have all the methods from the java.lang.Thread class + the run() method you we wrote in the thread class.

But, what if we would want to change the default implementation for one of the methods that come from the Thread class? With the first option you cannot... To do this, the second option is given.
Option 2:

Make your thread class extend from java.lang.Thread and provide new implementations for the methods by overriding them.

Hope this is clear..

About | Sitemap | Contact