• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About Multithreading in java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is Runnable interface is better or Extending thread is better.Please clarify in detail why it is better.
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajesh raju:
Is Runnable interface is better or Extending thread is better.Please clarify in detail why it is better.



Thread class also implements Runnable interface only.
If you see the source code of Thread Class.You can see this line



So i think both the things has the same effect.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The big difference is reusability.

A thread can be run only once; after it has finished, calling start again will result in an exception. You can reuse a Runnable in multiple threads though, even at the same time.

Of course this still requires creating a thread for each time you need to run it, so in the end the number of objects created will be more or less the same.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding on top of what Rob Prime told, a simple way to get another advantage is by implementing an interface my class is free to extend some other business class
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have extended Thread when I wanted to make use of the ThreadGroup as a simple way to track the status of working Threads.

Bill
 
Ranch Hand
Posts: 43
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A thread should be extended only when its behaviour has to be extended/modified(which is very rare).
The general requirement is to execute a task/group-of-statements on a seperate thread. Implementing Runnable is the right solution for this requirment.

Refer to Thread FAQ for more details.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic