7

I'm trying to find out what are the possible advantages of extending the Thread class would be?

This is a part of another question which I describe: There is two way of creating threads in Java

  1. extending from Thread class
  2. implementing the runnable interface

As expalined here there are several benefits of using runnable interface. My question is what is the advantage of extending from Thread class? The only advantage that comes to my mind is that one can extend from Thread class, and let's say call it ThreadExtended class. Then he/she can add more functionality in ThreadExtended(which I don't know what that might be), and then when he/she wants to create a thread, instead of extending from Thread class, it extends from ThreadExtended.

Is there any advantages in using Thread class instead of Runnable interface? Do you know any class(es) that extends from Thread class and then ask users to extends from those classes if they want to have multithreading capabilities?

    public class ThreadExtended extends Thread{
    //override some functions || add more functionality to Thread class
    }

    public class MyThread extends ThreadExtended{
     public void run()
    {
     for(int i=0;i<10;i++)
        {
          System.out.println("Using ThreadExtended instead of Thread directly");
        }
    }

    public static void main(String args[])
    {
    MyThread myThread = new MyThread();
    myThread.start();
    }
    }
Community
  • 1
  • 1
sheidaei
  • 9,842
  • 20
  • 63
  • 86

5 Answers5

9

There is rarely a compelling reason to extend the Thread class. I would imagine that in most cases, you would end up just throwing all of your 'do some stuff' logic into the run method anyway.

You should definitely stick with implementing Runnable. By choosing to extend Thread, you are creating a class hierarchy that probably is nonsensical and will wind up restricting your options for refactoring things in the future. By choosing to implement Runnable, you make no demands of what the lineage of the implementer is, and you can use powerful abstractions like the ExecutorService to abstract away the nuts and bolts of running a chunk of code. Finally, preferring to implement an interface rather than extend a class is a good practice!

5

The only reason to extend Thread is if you need to add behavior that is associated with the thread itself, rather than the task that the thread is executing.

For example, if you're implementing a thread pool (which nobody should do anymore, given java.util.concurrent), you would need to change the behavior of the thread so that (1) it can accept new work, and (2) it returns itself to the pool. In a very simplified form:

public void setRunnable(Runnable runnable) {
    this.runnable = runnable;
}

public void run() {
    while (true) {
        // wait on lock
        try {
            this.runnable.run();
        }
        catch (Throwable ex) {
            // do something with exception
        }
        finally {
            // return to pool
        }
    }
}
parsifal
  • 646
  • 3
  • 4
1

I find it clearer to extend Thread if I also configure the thread, for instance:

class FileReaper extends Thread {
    FileReaper() {
        setDaemon(true);
        setName(getClass().getSimpleName());
    }

    @Override public void run() {
        // do something
    }
}
meriton
  • 68,356
  • 14
  • 108
  • 175
0

Simply put, when you extend Thread that will be the only class you will be extending from!

Dumbo
  • 13,555
  • 54
  • 184
  • 288
0

There is also a good reason to extend a Thread - if you want to create a Looper Thread:

This is a typical example of the implementation of a Looper thread, using the separation of prepare() and loop() to create an initial Handler to communicate with the Looper.

  class LooperThread extends Thread {
      public Handler mHandler;

      public void run() {
          Looper.prepare();

          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };

          Looper.loop();
      }
  }

The UI thread is a Looper Thread, but you might want to create your own worker Looper Thread. To learn how the Thread with a Looper running its loop() method on it behaves, see my recent answer here.

Varvara Kalinina
  • 2,043
  • 19
  • 29