Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Multithreading in java

 
Rajesh raju
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is Runnable interface is better or Extending thread is better.Please clarify in detail why it is better.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • 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
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic