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
posted
0
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
posted
0
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.