11

I am new to thread programming in Java and hence this basic question. (I checked, but could not find this question previously asked)

I read that threads can be created either by inheriting the Thread class or by implementing the Runnable interface. I saw a code which had both to the same class.

public class ThreadExample extends Thread implements Runnable {
}

I was wondering what kind of situation would want this and if there is any advantage to this, what is it.

msrd0
  • 7,816
  • 9
  • 47
  • 82
noMAD
  • 7,744
  • 19
  • 56
  • 94

5 Answers5

12

extending Thread and implementing Runnable is useless (Thread already implements Runnable). You pretty much always want to implement Runnable (and not extend Thread). That gives you the flexibility of using a Thread directly (not recommended) or using one of the newer ThreadPool implementations in java.util.concurrent (recommended).

Graham
  • 7,431
  • 18
  • 59
  • 84
jtahlborn
  • 52,909
  • 5
  • 76
  • 118
5

No, There is no advantage of using this approach, because, Thread class implements Runnable interface. So, if your class extends Thread class. It means, it's also implementing Runnable interface.

http://www.developerfusion.com/pix/articleimages/may05/javathread3.jpg

mogli
  • 1,549
  • 4
  • 29
  • 57
4

IN this specific situation it's not very useful, as other posters have explained it's fairly obvious that Thread already implements Runnable.

In some cases, "stating the obvious" can be useful though, just as a "reminder" to the user of your class: if you have a fairly large hierarchy of super-classes and interfaces, with several levels of inheritance (some of them in 3rd-party libraries), it could be useful as a helper to declare a class as implementing a specific interface, even though it implements it by definition because its superclass already implements it or implements one of the sub-class of that interface.

It's specially useful with marker interfaces (some people might object they should not be used at all, and they are bad practices - well sometimes you don't control the environment fully), i.e. interfaces with no actual implementation and just designed to mark your object eligible for a special function (e.g. Cloneable). In such a case, marking each of the allowed classes even though their parents are already eligible can be more explicit, so more useful.

Guillaume
  • 22,694
  • 14
  • 56
  • 70
-1

Runnable interface means assigning one job to many threads while Thread class,each of threads has a unique object associated with it.

Jalpa
  • 1
  • this is in no way even remotely correct –  Aug 04 '17 at 17:20
  • Please read [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) before attempting to answer more questions. –  Aug 04 '17 at 17:20
-1

Implementing runnable interface would be preferable when you need needs more flexibility of extending other base classes.

rashedcs
  • 3,588
  • 2
  • 39
  • 40
  • Please read [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) before attempting to answer more questions. –  Aug 04 '17 at 17:20
  • I already read that. Please tell me - What is the wrong in my answer? – rashedcs Aug 04 '17 at 21:01