wood burning stoves*
The moose likes Threads and Synchronization and the fly likes Will JVM creates a new native thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Will JVM creates a new native thread?" Watch "Will JVM creates a new native thread?" New topic
Author

Will JVM creates a new native thread?

Suhas Madap
Ranch Hand

Joined: Aug 29, 2006
Posts: 52
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.


Thank you,<br /> <br />Suhas K Madap<br /> <br />If you understand what you're doing, you're not learning anything."
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 29, 2006
Posts: 52
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Will JVM creates a new native thread?
 
Similar Threads
Predicting output of threads Problem
Thread problem
Threads
Issue regarding Executing Thread
JVM