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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Performance" Watch "Thread Performance" New topic

Thread Performance

Karthik Gupta

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


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.
subject: Thread Performance
It's not a secret anymore!