Skip to main content

Posts

Showing posts from December, 2015

If Runnable interface is better than Thread class, then why we are using Thread class?

If Runnable interface is better than Thread class, than why we are using Thread class? What is the need for Thread class? When you  extends Thread class, after that you can’t extend any other class which you needed. (As you know, Java does not allow multiple inheritance.) If your class is Implementing the Runnable interface then you only override the run() .So this instance creates a separate Thread and every individual Thread runs separately but not as a single heavy Thread in your program. Another thing, Since Java does not support multiple inheritance , if you implement the Runnable you'll avoid problems of multiple extending, so if you implement Runnable interface you can extend any class that you are required other than Thread class. However, the main difference is. When you  extends Thread class, each of your thread creates unique object and associate with it. When you  implements Runnable , it shares the same object to multiple threads. ...