• support@answerspoint.com

“implements Runnable” vs. “extends Thread”

694

From what time I've spent with threads in Java, I've found these two ways to write threads:

With implements Runnable:

public class ThreadA implements Runnable {
    public void run() {
        //Code
    }
}
//Started with a "new Thread(threadA).start()" call

Or, with extends Thread:

public class ThreadB extends Thread {
    public ThreadB() {
        super("ThreadB");
    }
    public void run() {
        //Code
    }
}
//Started with a "threadB.start()" call

Is there any significant difference in these two blocks of code ?

2Answer


0

Yes: implements Runnable is the preferred way to do it, IMO. You're not really specialising the thread's behaviour. You're just giving it something to run. That means composition is thephilosophically "purer" way to go.

In practical terms, it means you can implement Runnable and extend from another class as well.

  • answered 2 years ago
  • Gul Hafiz

0

If you want to implements or extends any other class then Runnable interface is most preferable other wise if you do not want any other class to extend or implement then Thread class is preferable

The most common difference is

enter image description here

When you extends Thread class, after that you can’t extend any other class which you required. (As you know, Java does not allow inheriting more than one class).

When you implements Runnable, you can save a space for your class to extend any other class in future or now.

  • Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java.

  • In Object oriented programming extending a class generally means adding new functionality, modifying or improving behaviors. If we are not making any modification on Thread than use Runnable interface instead.

  • Runnable interface represent a Task which can be executed by either plain Thread or Executors or any other means. so logical separation of Task as Runnable than Thread is good design decision.

  • Separating task as Runnable means we can reuse the task and also has liberty to execute it from different means. since you can not restart a Thread once it completes. again Runnable vs Thread for task, Runnable is winner.

  • Java designer recognizes this and that's why Executors accept Runnable as Task and they have worker thread which executes those task.

  • Inheriting all Thread methods are additional overhead just for representing a Task which can can be done easily with Runnable.

Courtesy from javarevisited.blogspot.com

These were some of notable difference between Thread and Runnable in Java, if you know any other differences on Thread vs Runnable than please share it via comments. I personally use Runnable over Thread for this scenario and recommends to use Runnable or Callable interface based on your requirement.

However, the significant difference is.

When you extends Thread class, each of your thread creates unique object and associate with it. When you implements Runnable, it shares the same object to multiple threads.

  • answered 2 years ago
  • Sunny Solu

Your Answer

    Facebook Share