0

We can create a thread using Implementing Runnable or by extending a thread class. Can anyone tell me performance wise which is faster implementing a Runnable or extending a Thread class and Why? Also I know while implementing Runnable we can implement more Interfaces and also extend a class. Extending a class is something we lose out if we directly extend a Thread class. But is there any performance difference in both scenarios

  • 2
    It isn't a performance issue. No measurable difference. Runnable should be preferred because you ought to be using new concurrency classes like Executor, not Thread. Those all take Runnables. There's a good reason for that. – duffymo May 04 '17 at 17:50
  • 1
    I believe there is no practical difference concerning "performance" (what do you mean by it? Faster execution, better memory consumption?). One of the main reasons there are both the Class and the Interface is that in Java you cannot have multiple inheritance, so depending on your design, your class may be already extending another class and all you need is to implement Runnable. – Vitor Santos May 04 '17 at 17:50
  • I mean faster execution. I know while implementing Runnable we can implement more Interfaces and also extend a class. Extending a class is something we lose out if we directly extend a Thread class. But is there any performance difference in both scenarios – user2821056 May 04 '17 at 17:56
  • 1
    'faster execution' at this point is the least important thing. never, ever, extend Thread unless you have to, and you only have to if you mean to change the basic functionality provided by the Thread class, but only in about 0.4% of possible cases, that's what you want. – Stultuske May 04 '17 at 18:03
  • see also http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread – Arne Burmeister May 04 '17 at 18:06
  • Don't extend `Thread`. Don't. Just don't. – Lew Bloch May 04 '17 at 18:13
  • @Stultuske, don't invent statistics. – Lew Bloch May 04 '17 at 18:18

1 Answers1

0

It depends (as always):

  • If you would run it only once, the Thread extension has no drawback except it is not as flexible and harder to test (e.g. in a unit test).
  • If you want to run it more often, the Runnable is more flexible as you can pass it to an Executor using a thread pool. By doing so you would get a better performance as the overhead of thread creation for each execution will be dropped.

If you implement a Runnable you have various ways to execute the code (synchronous in a unit test, using an executor, or even wrap it with a Thread anyway). And the pure thread extension never has a better performance, it can be equal only. Implementing Runnable only is always the better option also by allowing multiple interfaces or using an independent inheritance, it is simply the better design. Following this I would never extend Thread.

Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94
  • The question is, the stuff you run *inside* `Thread` should only extend `Thread` if your type _is-a_ `Thread`, which it isn't. It _is-a_ `Runnable`, however. So the question isn't "which performs better?" That's a terrible question. A good question is, "Which is correct?", and "Which leads to fewer bugs?" Correct engineering trumps uncertain micro-"optimization" every time. Do it right and let "performance" take care of itself. – Lew Bloch May 04 '17 at 18:16