Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Performance

 
Karthik Gupta
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic