• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Class and Runnable Interface  RSS feed

 
Ayan Dutta
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from extending multiple classes why Runnable Interface is used ?
I have read from net that Thread class abstracts the concept of worker where as Runnable s abstracts the concept of work .But I did not understand anything .Can somebody explain this ?
In which situation which is better ? Can anybody give some examples in Java itself ?
Regards,
Ayan Dutta
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ayan Dutta:
Apart from extending multiple classes why Runnable Interface is used ?
I have read from net that Thread class abstracts the concept of worker where as Runnable s abstracts the concept of work .But I did not understand anything .Can somebody explain this ?
In which situation which is better ? Can anybody give some examples in Java itself ?
Regards,
Ayan Dutta


Generally a Runnable is better because it is more versatile: you can hand-off to a specific thread, execute directly, post to the event queue, submit to a pool, store in a channel, transmit via RMI, serialize (potentially) etc etc.

Of course if you are defining a very specific task to be run in a very specific thread then defining a custom Thread subclass, perhaps with additional utility methods, is fine.

Well practically speaking, I found every time implementing Runnable interface is more efficient rather than, extending Thread class. Although there is not such a differnce.

The only difference is when you extend Thread class, now your class can only act as thread it cann't extend any other class if you would have any wish like that.And that's why it abstract as worker

But while implementing Runnable Interface you are free to extend any other class. and that's why it abstracts as work

That's the only difference I feel .....
Please let me know if there are any issues.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well as you eluded to you need to use runnable if you thread already extends another class because you couldn't also extend Thread.

If you look Thread you will see it implements Runnable so this should give you a hint about how closely related they are. If you extend Thread then you should override run() if you implement runnable then you must implement run(). Either way a new Thread instance is created (new MyThread() or new Thread(new myRunnable())) and your run() method gets called.

I personally just tend to use runnable because I don't see any reason to subclass Thread and if my runnable needs to subclass something in the future I don't have to rework it.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have extended Thread when I wanted to use ThreadGroup as a convenient way to keep track of all running Threads.
Bill
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim LeMaster:

I personally just tend to use runnable because I don't see any reason to subclass Thread and if my runnable needs to subclass something in the future I don't have to rework it.


Hey Tim, you mande valueable point. Thanks!
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
I have extended Thread when I wanted to use ThreadGroup as a convenient way to keep track of all running Threads.
Bill




I believe this code will let you do that without extending Thread.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it was a bit more complicated than just being able to get the names of running Threads. My class extending Thread had various status variables I could get at by enumerating through the list returned by the ThreadGroup enumerate method.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!