Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "implements Runnable" is Preferred over "extends Thread"? [duplicate]

The Java Thread itself implements a Java Runnable! and according to most of the experts over Internet, implements Runnable is preferred over extends Thread! even though we cannot use utilize Runnable in the sense of thread with out the Thread class!
Then why do we prefer to implement Runnable over extending Thread since in both cases the actual thread is stated by calling a Thread implemented method (i.e. start() or run()) although in case of Thread we are not truly "extending" the functionality of Thread by merely overriding the run() method?

I apologies if I sound confusing..!

like image 609
Tariq Avatar asked Mar 18 '13 07:03

Tariq


People also ask

Why is it preferred to implement runnable instead of extending thread?

When we extend Thread class, we can't extend any other class even we require and When we implement Runnable, we can save a space for our class to extend any other class in future or now.

What is the advantage of creating a thread with runnable interface than by extending a thread class?

using Runnable interface give a chance to extend another class. Whereas, Extending Thread class doesn't. Saving memory using Runnable interface in case of multiple threads requirements. We don't have to create multiple objects of our thread class.

Why does Java have the runnable interface instead of having classes extend from thread?

Java only supports single inheritance, so you can only extend one class. Instantiating an interface gives a cleaner separation between your code and the implementation of threads. Implementing Runnable makes your class more flexible. If you extend Thread then the action you're doing is always going to be in a thread.

Why do we implement runnable interface in Java?

The runnable interface is used to write applications which can run in a separate thread. Any class that implements the runnable interface is called a thread. The code of the thread is executed by the interpreter after the thread is started.


2 Answers

The most common difference is:

When you extend Thread class, you can’t extend any other class which you require. (As you know, Java does not allow inheriting more than one class). When you implement Runnable, you can save a space for your class to extend any other class in future or now.

However, the significant 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.

Check this: http://manikandanmv.wordpress.com/tag/extends-thread-vs-implements-runnable/

like image 115
Aashray Avatar answered Oct 02 '22 15:10

Aashray


If your class is extending the Thread class then it becomes a single thread which inherits the properties Thread class, so it'll be heavy. (When extending Thread class each of the threads creates unique object and associate with it, but when implementing Runnable, it shares the same object to multiple Threads).

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.

like image 45
Maroun Avatar answered Oct 02 '22 16:10

Maroun