-1

First of all i have gone through similar question but couldn't get my answer, like - Why does Thread implement Runnable?

So my question is that Runnable contains only run method which is there is Thread class itself then why does Thread Class implements Runnable Interface and what functionalities does implementing Runnable provides to Thread class, what will happen if Thread class does not implements Runnable.

  • 4
    What **specifically** about StephenC's answer to that other question doesn't address your question? It certainly seems to to me. – T.J. Crowder Sep 26 '17 at 12:25
  • `Runnable r = new Thread ();` <-- That's something you wouldn't be able to do if it didn't implement Runnable. Is that what you're looking for? – yshavit Sep 26 '17 at 12:30
  • @yshavit - not sure what that means, would be thankful if you can explain what does that statement means ( creating a thread object with Runnable reference, how the behavior different ) and all i want to know is the reason/need for implementing Runnable – qwasd wsdcx Sep 26 '17 at 12:36
  • From Thread class documentation, `public void run(): If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.` Is this what you what? – CS_noob Sep 26 '17 at 12:39
  • 2
    I don't mean to be harsh, but if you don't understand what my snippet means, I think you need to go back and (re-)read a Java tutorial. It's basic polymorphism, which is a fundamental Java concept that you should make sure you understand. – yshavit Sep 26 '17 at 12:54
  • @yshavit: why should anyone write `Runnable r = new Thread();`? That would be advertising to call `run()` on the object, which is exactly what you shouldn’t do. But anyway, I thought the already linked Q&A does already tell that it is merely a historical nuisance, doesn’t it? – Holger Sep 26 '17 at 13:30
  • @Holger You wouldn't want to, for exactly that reason. But you _could_, which means someone almost definitely has, and Sun/Oracle have been extremely careful not to break backwards compatibility. But the linked dupe already talks about that history, and the OP mentioned it, so I was trying to guess at what they might want that's not covered in that question. The only thing I could think of was a concrete example of something that you could do only if Thread implements Runnable. – yshavit Sep 26 '17 at 15:02

2 Answers2

0

"The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. [...] This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread."

oracle doc

But I think what your exactly looking for is here. The answer given was "backward compability". Sometimes Java needs to make choices, and they always chose solutions dealing with backward compability.

B.vlt
  • 16
  • 4
-1

If Thread class doesn't implement Runnable then Thread class will not have run method. Then jvm will not treat it as a thread at all.

Dibyendu
  • 41
  • 6