Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
    Bookmark Topic Watch Topic
  • New Topic

Thread class or Runnable  RSS feed

 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Report post to moderator
Which is better in terms of performance
Thread class or Runnable interface and why?

Please help
Thanks in advance
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23279
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
deepak munjlani
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!