Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 3946
    
  17

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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing Runnable or Extending Thread class
 
Similar Threads
Thread class
Thread class
extend or implement?
Extending Thread or implementing Runnable
best way to create a thread