This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Implementing Runnable or Extending Thread class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implementing Runnable or Extending Thread class" Watch "Implementing Runnable or Extending Thread class" New topic
Author

Implementing Runnable or Extending Thread class

ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Hi,

I want to know which is the best way to create a thread in java

1) extending Thread Class

OR
2) implementing Runnable Interface

???

As both work in a similar manner, i want to know which 1 is best ?

Cheers,

Ujjwal B Soni
<baroda, Gujarat, India>
<919998971048>


Cheers!!!
Ujjwal B Soni <baroda, gujarat, india> <+919909981973>
"Helping hands are better than praying lips......"
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Implement Runnable. You should only extend thread if you are adding more functionality to it, or attempting to change the way it behaves. Most of the time you are just trying to get code to run in a thread, not change how the Thread works. That is what the Runnable is for.

Theory aside, the Runnable also gives you more flexibility at lower cost. You can use Runnables with Executors in the java.util.concurrent package, and in Thread pools available from many sources. And you don't get any of the overhead that a Thread has when implementing it.


Steve
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Thanks for the reply!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Implementing Runnable or Extending Thread class
 
Similar Threads
best way to create a thread
Thread class
Extending Thread or implementing Runnable
Thread class
extend or implement?