11

I know there are two ways to use a thread in java:

  1. implement Runable
  2. extend Thread

I also know implementing Runable is better than extending Thread.

But why there are two ways - why not only one?

If implementing Runnable is a better approach, why is there another option?

What would be wrong with having only one option ?

jahroy
  • 22,322
  • 9
  • 59
  • 108
TKumar
  • 818
  • 2
  • 10
  • 35
  • http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread?lq=1 check this. – Aarun Jun 26 '13 at 05:13
  • Here's a [good related article...](http://www.java-forums.org/blogs/java-tip/340-creating-thread-implementing-java-runnable-interface.html) – jahroy Jun 26 '13 at 09:10

10 Answers10

17
  • extends Thread:

    your thread creates unique object and associate with it

  • implements Runnable:

    it shares the same object to multiple threads

Another thing to note, since you can extend only one class in Java, if you extends Thread, you can't extend another class. If you choose to implement Runnable, you can extend class then.

Maroun
  • 94,125
  • 30
  • 188
  • 241
7

Technically, there is only one way: implement Runnable. Thread, incidentally, does just that, so extending it you trivially satisfy the interface requirement.

Amadan
  • 191,408
  • 23
  • 240
  • 301
3

Just another reason why we use each type of threading.

Extending Thread class will not give you an option to extend any other class. But if you implement Runnable interface you could extend other classes in your class..

So depending on your design requirement you could use either of the menthods.

blganesh101
  • 3,647
  • 1
  • 24
  • 44
2

Subclassing Thread class allows you to modify other overridable functions of the Thread class, should you wish to do so. You, probably, shouldn't.

Also, subclassing Thread class can result in a more readable code sometimes where in your subclass you can have your own custom API. One can imagine classes such as DownloadingThread, RenderingThread etc extending Thread.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
2

Using Thread as a task provides a compact way to create and run a parallel thread

new Thread() {
    public void run() {
        ...
    }
}.start();
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
2

Both methods have different approaches. Implementing Runnable interface does not give any control over thread itself.And if we extends thread class then derived class can not extend any other base class.

So if user wants fully control over program then Extending of Thread class is better option and if user wants flexibility of extending other base classes then Implementing Runnable Interface is good option.

Manish Doshi
  • 1,205
  • 1
  • 9
  • 17
2

Even if you implement Runnable interface you will need to create thread to let your task run as a thread. obvious advantages you get out implementing Runnable are

  1. You have liberty to extend any other class
  2. You can implement more interfaces
  3. You can use you Runnable implementation in thread pools

Extending Thread class is just an option as it implements Runnable internally so you end up implementing Runnable interface indirectly. Its just that this class was not made final to prevent developers from extending it. As Joshua Bloch mentions in 'Effective Java' that there should be no reason to extend Thread usually

Saurabh
  • 7,894
  • 2
  • 23
  • 31
2

By extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same runnable instance.

Rajan Garg
  • 317
  • 1
  • 3
  • 7
1

Most of the time, we use runnable interface. Because that allows us more flexible on the structure and functionality. Java allows multiple inheritance for interface. That concept is used quite a lot in Software Design.

By the way, there is another interface called callable. A callable class could return value in type V and also may throw checked exceptions.

Jeremy Zhou
  • 49
  • 1
  • 4
1

When we create thread by implements runnable we don't initialize any value during thread creation. But when we extend Thread class to create thread we can initialize some value if needed using the advantage of constructor as follw

public class MyThread extends Thread
{
 int aValue;
 public Mythread(int aValue)
 {
 this.aValue = aValue; 
 }
 ............................
 ............................

}

When we create thread we can initialize as follow

MyThread t = new MyThread(7);
t.start();

Beside that since java doesn't support Multiple inheritance so we if extends Thread class then we lost our opportunity to extends another class.Hence in this scenario runnable interface is so helpful for creating thread

Sajal Saha
  • 169
  • 2
  • 12
  • Thanks.. for comments. It is also the important term of the question. – TKumar Jun 26 '13 at 07:04
  • -1 You can definitely initialize variables in classes that implement Runnable. Here's [an article](http://www.java-forums.org/blogs/java-tip/340-creating-thread-implementing-java-runnable-interface.html) with an example. It's also worth reading for its explanation of the benefits of and differences between "_implementing Runnable_" **vs** "_extending Thread_". – jahroy Jun 26 '13 at 09:06