0

Generally thread can be created in 2 ways

  1. Extending a thread class
  2. Implementing a Runnable interface

By reading in all tutorials, all says that implementing a Runnable interface is always good since you cannot extend any other class if you create by extending(1st method) I agree. And also if we create thread by extending then every thread will create new object whereas if we create thread by implementing all thread will share the same object.

My question is if implementing Runnable is best option and extending thread is bad practice why java have this option? I believe there must be surely some advantage if we create thread by extending ( I believe we should use extending thread method, when there is no other parent class is not the only answer )

Thanks in advance

Guest
  • 21
  • 8
  • Thread implements Runnable internally, so it is very clear that, Implementing interface will far more flexible and easy to plug with modification if needed, other than having extending class with tightly couple architecture. – Simmant Aug 20 '18 at 05:53
  • For more please have a look of Thread implementation, http://hg.openjdk.java.net/jdk6/jdk6/jdk/file/5672a2be515a/src/share/classes/java/lang/Thread.java – Simmant Aug 20 '18 at 05:54

3 Answers3

5

In 2018, the real answer is: you don't use bare metal threads at all.

We have abstractions like the fork/join, futures, or the ExecutorService framework these days.

Avoid doing threads yourself!

If you do, you consider the "good old" Favor Composition over inheritance. Which, in this case boils down to: don't extend thread, but implement Runnable. You do not want to restrict your class by using extends, when you could get away with implements instead!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    I think you're right, but, this is more of a comment than an answer, since it doesn't attempt to answer the OP's question. – Erwin Bolwidt Aug 20 '18 at 04:22
  • @ErwinBolwidt Sure, I see your point. On the other hand, the more people understand: "dont use threads" in the future, the better. And answers have a better chance to find readers. Beyond that, I further enhanced my answer to be a bit more helpful. – GhostCat Aug 20 '18 at 09:13
2

This is a design decision, when you should implement a Runnable interface or when you should extend a thread.

  1. Ask some question yourself - If you want a object that is more specific or specialized version of object. If it adds new functionality to the existing Thread object, then you should go for extending a threda class. Else go for implementing a Runnable interface.
  2. Implement a Runnable does not create a Thread. Implementing a Runnable interface merely createsa task that should be executed by a thread.
  3. Another benefit of implementing a task is that task is reusable while object created using the thread extension is not reusable.
Dinesh
  • 1,046
  • 1
  • 8
  • 17
0

To correct, thread can be created in 3 ways

3. Using Callable interface

Java does not allow a class to extend multiple class to prevent diamond problem that was there in C/ C++. If you are sure your class will not extend any other class but Thread you can happily extend from Thread class or it is advisable to extend Runnable or Callable interface

I believe there must be surely some advantage if we create thread by extending

You get all the advantages of extending a class, in case you want to provide custom implementation for a method.

Vishrant
  • 15,456
  • 11
  • 71
  • 120