• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extending thread class

 
badri nath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Can you let me know the advantage of using extends thread class over implements runnable interface while creating a thread.I guess the second one is mostly adopted practice while creating a thread. But there must be some advantage, if we adopt the first approach.
 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think there is any significant advantage that you'll get by extending Thread rather than implementing runnable. Since you cannot extend more than one class in Java, its always better to implement runnable so that you can extend any other class (if required)

Cheers,
Arvind
 
badri nath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arvind
Ya i know that implementing is best practice as you rightly said if we extend thread class then we will not be able to extend any other class. But if there is approach then it might be suitable in some applications.As of now im not able to know in what type of situations it is required.
Please, can any one make this clear by showing an example.
With Regards Badri
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by badri nath:
But if there is approach then it might be suitable in some applications.As of now im not able to know in what type of situations it is required.

??? It's not clear what you are trying to say.

The preferred approach is to implement Runnable versus subclassing Thread. While I acknowledge the argument that one could simultaneously subclass X and implement Runnable, I think the *main* reason for implementing Runnable is simplicity -- keep it simple! One rarely needs to override Thread methods besides run(), so subclassing Thread is overkill. Indeed, the fact that Thread implements Runnable is widely considered to be a mistake in design -- confusing the task with the thread that executes the task.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by badri nath:
Hi Arvind
Ya i know that implementing is best practice as you rightly said if we extend thread class then we will not be able to extend any other class. But if there is approach then it might be suitable in some applications.As of now im not able to know in what type of situations it is required.
Please, can any one make this clear by showing an example.
With Regards Badri


There is no scenario that mandates extending Thread rather than implementing Runnable that I am aware of. However, I have created a ThreadWorker that maintained a BlockingQueue of Runnables and ran them as they were made available via an execute(Runnable) method. Since there was an "is a" relationship there and I considered the ThreadWorker a Thread I made it inherit. That way I could set the priority of a ThreadWorker, for example, or make a ThreadWorker that was a daemon Thread, etc. So I would say that if you want your class to be a Thread, rather than simply run in one, inherit it, but that's a very rare scenario and could be similary accomplished with a Runnable implementation.
 
Parag Ballal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if there is approach then it might be suitable in some applications.As of now im not able to know in what type of situations it is required.
Please, can any one make this clear by showing an example.


There is no scenario that mandates extending Thread rather than implementing Runnable that I am aware of.


If a design requires Applet object to act as a Thread, then using Runnable inteface becomes obligatory.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Parag Ballal:
If a design requires Applet object to act as a Thread, then using Runnable inteface becomes obligatory.


And there would be something I wasn't aware of as I don't write applets. Thanks.
 
shailesh Jammie
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When we will use the approach of extending Thread class where we need a class fully dedicated to the particular thread operation.
let us take an example:
for an application we have to show progress bar according to the
progress of a work like fetching large data from database.

So what we will do is we can create two classes:
one main class fetching the data from db and according to the status of fetched data it will send this progress to the another class dedicated
to display the progess bar in the screen.
By this way the second class is fully dedicated thread of displaying progress bar diffrent from main operation of fetching data.

i hope you understood.

regards
Shailesh
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shailesh Jammie:
Hi,
When we will use the approach of extending Thread class where we need a class fully dedicated to the particular thread operation.
let us take an example:
for an application we have to show progress bar according to the
progress of a work like fetching large data from database.

So what we will do is we can create two classes:
one main class fetching the data from db and according to the status of fetched data it will send this progress to the another class dedicated
to display the progess bar in the screen.
By this way the second class is fully dedicated thread of displaying progress bar diffrent from main operation of fetching data.

i hope you understood.

regards
Shailesh



Are you writing that in this example one *must* extend Thread rather than implement Runnable? If so, I don't see how you are demonstrating that.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shailesh Jammie:
Hi,
When we will use the approach of extending Thread class where we need a class fully dedicated to the particular thread operation.
let us take an example:
for an application we have to show progress bar according to the
progress of a work like fetching large data from database.

So what we will do is we can create two classes:
one main class fetching the data from db and according to the status of fetched data it will send this progress to the another class dedicated
to display the progess bar in the screen.
By this way the second class is fully dedicated thread of displaying progress bar diffrent from main operation of fetching data.

i hope you understood.

regards
Shailesh


This is an example where extending Thread would very likely be quite inappropriate. In fact, that's a very common problem when using AWT/Swing and that's exactly the opposite of what you should do. Instead you would pass a Runnable to the EDT for execution in that thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!