Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What are differences in the two ways of creating threads  RSS feed

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all ,

Can anyone explain the differences in the two ways of creating threads beside the fact that when we implement Runnable during thread creation the class can extend some other class which is not possible in case of thread creation by extending the thread class . In what scenario would we prefer creating a thread by implementing the Runnable interface and in which scenario is extending the Thread class preferred .
 
Mohamed Sanaulla
Bartender
Posts: 3169
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the reason
ragi singh wrote: we implement Runnable during thread creation the class can extend some other class which is not possible in case of thread creation by extending the thread class


why

ragi singh wrote: we prefer creating a thread by implementing the Runnable interface and in which scenario is extending the Thread class preferred .


You should be knowing that even Thread implements Runnable
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sanaulla ,

I think this is not the only reason , besides i would like t o know the scenario in which either is used ?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We even have an FAQ entry about this ExtendingThreadVsImplementingRunnable.
 
Arulkumaran Nedumaran
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we create a thread, JVM does a lot of stuff like initializing the lifecycle of it because thread is not just like any other java object. It is a specialized object which is capable of running on its own. So if you think in this perspective, creating a thread by extending the Thread class is heavy work for the JVM than doing it by passing a Runnable object to existing threads in a pool. So in a concurrent environment it is always recomended to implement Runnable interface than extending a thread.

And ofcourse, another reason is you extend a class only to provide some extra functionality to the super class. If you are not going to create a specialized Thread class by extending it, in the inheritance perspective, its not right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!