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
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).
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.
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 3 years ago
- Sunny Solu