• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Will JVM creates a new native thread?

 
Suhas Madap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

May I know whether JVM creates a native thread or not, when SubThread doesn't override run()? Any explanation related to behavior of JVM for the below code is appreciated.

Sub Thread Class with Out run()


Main Class


Thanks in advance,
Suhas Madap.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will create a new thread of execution when you call start(). Exactly what that means is entirely up to the implementation, but there's no reason to think it will behave any differently for a subclass of Thread than for Thread itself.

However, you should rarely, if ever, have a good reason to subclass Thread. Why are you doing it here?

And even if you do subclass Thread, there should rarely, if ever, be a good reason for overriding its run() method.
 
Suhas Madap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

Thanks a lot!! for the explanation. This was just an experimental to understand JVM behavior. I completely agree with you, it would be a rarest use-case where sub thread will not override run().
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suhas Madap wrote:Hi Jeff,

Thanks a lot!! for the explanation. This was just an experimental to understand JVM behavior. I completely agree with you, it would be a rarest use-case where sub thread will not override run().


No, it would be a rare case where a subclass of Thread does override run(). It's a design mistake for Thread to even have a run() method in the first place. Use runnable() for that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic