difference between implementing runnable and extending thread

Looks like you are learning threading and threads in Java. This means you are now basic in Java. You might have learn that you need to either extend Thread class or implement runnable in order to use threading. You might want to find some differences while you use these two then I will make some points on why they differ.

First why to implement or extend.

You might know that you can just extend one class while you can implement as many times you want. So depends on your needs whether you want to extend or implement. If you are extending some other class then you need to implement runnable or you can continue extending the class.

Few differences you need to remember while you do these two. In both of the cases, you need to override the run method but while you make a new thread, you need to take care of few things.

While you extend Thread class you make a new thread like this.

Thread threadname = new classthatextendsthread();

While you implement runnable, you make a new thread like this.

classthatextendsthread newobj=new classthatextendsthread();
Thread threadname= new Thread(newobj) // you need to pass object as argument.

This way you need to notice the difference.


Get more difference click me.

 

Information Sharing Copyright © 2011