Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread vs Runnable

 
Gourab Guha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I know the basic difference between Thread and Runnable. If my class is extending some class but still we need to make it Thread, then we should use Runnable otherwise we can use any one.
But specifically I want to know is extending Thread gives any added advantage rather then implementing Runnable.
Please let me know the answer.

Regards,
Gourab
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is often asked and we haven't come up with any good reason to extend Thread. In general, extending a concrete class is a dangerous thing, so implementing Runnable is A Good Thing.

I had a routine that had to keep references to Threads to join() and to the Runnables to get data and it was a bit of a pain to keep both. That was the only time in recent memory I was tempted to extend Thread. I still didn't.
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question address the design aspects of programming. What you have specified is a rule of thumb about whether or not to extend Thread.

I would recommend that you understand the theoretical differences between classes and interfaces and how to implement that theory into practical situations. This is less related to the Thread class or the Runnable interface and more related to how to actually design a *good* system.

Goes to the core differences between classes and interfaces.

Yeah, that kind of stuff. This question is better answered (by yourself!) by stepping back and taking a wider perspective at the problem in hand.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure that you are not interested to change the behavior of a thread rather want to associate a worker with a thread.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The chief advantage of implementing an interface is that it enables the implementing class to extend any other class and still implement threading. But that depends on the requirements.

Another advantage could be that by implementing an interface, the class is forced to provide an implementation for the run() method, which is responsible for multithreading, while if you extend the class, you are not forced to override the run(), in case of which, a call to start() on the thread object will do nothing sort of multithreading.

Njoy!!
Sid
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sidd Cool:
The chief advantage of implementing an interface is that it enables the implementing class to extend any other class and still implement threading. But that depends on the requirements.

Another advantage could be that by implementing an interface, the class is forced to provide an implementation for the run() method, which is responsible for multithreading, while if you extend the class, you are not forced to override the run(), in case of which, a call to start() on the thread object will do nothing sort of multithreading.

Njoy!!
Sid


All true, but these are implementation advantages. There are more important reasons which undoubtidly are listed in the FAQ cited.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic