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 Threads and Synchronization and the fly likes Thread Performance 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 » Threads and Synchronization
Bookmark "Thread Performance" Watch "Thread Performance" New topic
Author

Thread Performance

Karthik Gupta
Greenhorn

Joined: Feb 17, 2005
Posts: 3
I am new to thread. I would like to know which one is better extending from Thread class or Implement Runnable interface in term of performance and why

thanks

Karthik
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I'm not sure it would really make a difference. Probably would have to code both up and run a profiler to be sure. From a design standpoint it is generally more acceptable to implement Runnable as you are not creating new types of Thread's, but rather building business logic that you want to run in a seperate Thread.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The only performance difference is that implementing Runnable with a separate class results in an extra class instantiation and a single method call. Both are trivial compared to anything that method might be doing and should cause no concern.

Design-wise, extend Thread only when you need to override the behavior of the Thread. If you just want a Thread to run some code, implement Runnable and pass it to a Thread. This gives you more options, for example using a thread pool and rerunning your Runnable multiple times.
 
jQuery in Action, 2nd edition
 
subject: Thread Performance
 
Similar Threads
Thread safe J2ee applcaition
Regular Expression Help
No
System.arraycopy VS toArray()
how to get heap dump for Java EE application/environment