• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

What are differences in the two ways of creating threads

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We even have an FAQ entry about this ExtendingThreadVsImplementingRunnable.
 
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic