aspose file tools
The moose likes Threads and Synchronization and the fly likes Thread class or Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply locked New topic
Author

Thread class or Runnable

Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
Which is better in terms of performance
Thread class or Runnable interface and why?

Please help
Thanks in advance


Jitendra
SCJP1.5
SCWCD1.5
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm sure there's not enough performance difference to worry about, or probably even measure. We generally prefer to implement Runnable because extending a concrete class like Thread has a lot of risks and because our own classes are not really special types of Thread but tasks that can be run on a thread.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16690
    
  19

I'm sure there's not enough performance difference to worry about, or probably even measure.


I agree. I also don't see any reason as to why one would be better, in terms of performance. But, I guess there is no harm to actually try it out.

Two cases... 100,000 instances of a class that inherits from the Thread class, with a run() method that does nothing... vs... 100,000 instances of a class that implements Runnable, passed to a Thread object, with a run() method that does nothing... in a loop, call start(), then join().

Total time around 17 to 18 seconds. On some runs, the first case is faster, on others, the second case is faster.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
deepak munjlani
Greenhorn

Joined: Mar 21, 2007
Posts: 2
hi friends,
I'm ok with whatever you said about the implementation part ,
But can anybody tell me why we need Thread Class when we already have Runnable interface..
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by deepak munjlani:
hi friends,
I'm ok with whatever you said about the implementation part ,
But can anybody tell me why we need Thread Class when we already have Runnable interface..


You already asked this in another post. Please don't cross-post the same question.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Thread class or Runnable
 
Similar Threads
doubt in wait() method.......
casting
Thread tricky questions.
Thread or Runnable ?
Does run() method is declard i Thread?