Search code examples
javamultithreadingrunnable

Why "implements Runnable" is Preferred over "extends Thread"?


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..!


Solution

  • 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/