Thursday, April 29, 2010

implements Runnable vs extends Thread:Java

In order to implement Multi-Threading in Java, one must implement the Runnable interface OR extend Thread class. I have read in many forums people asking which one to use, does one of the above ways have any advantages over the other, similar questions.

I also sometimes pondered on this. I just thought for a while, i realized that one cannot extend more than one class in Java (unlike C++).

So, if one has to write a class that does multi threading, one has to prefer doing the same using the implementing the Runnable interface as one is not really worried about "extending" features of Thread class, rather we are interested in using multi-threading features.

It also adds up that, implementing an interface gives us an option of extending a class. But if, we extend a class(Thread) we do not have an option of extending other class (if required).

Here is a good article that i found on Google:

No comments:

Post a Comment