aspose file tools*
The moose likes Beginning Java and the fly likes difference between start() and run() of a thread instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between start() and run() of a thread instance" Watch "difference between start() and run() of a thread instance" New topic
Author

difference between start() and run() of a thread instance

Phone Myat Kyaw
Ranch Hand

Joined: Dec 26, 2008
Posts: 31
Hi,

I'm studying the Thread area for the SCJP 6.0 exam and got a question that what is the difference between run() and start() of a thread instance? Are the both ways getting a new stack of thread ? For start(), what I understand is, the thread becomes runnable state, but how is run() ? Is it running directly on top of main thread like calling a method of its own or creating a new stack ?



Both gave me nothing wrong and I just wanna check how it's different. But when I changed the style (implements Runnable), the class cannot invoke start() (this is understandable) but still can invoke run(). Does it make a new stack of thread or on top of the main thread ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

When you call run() on a Thread reference, the run() method will run in the same thread. The start() method creates a new thread (not an instance of the class but the actual concurrent thing), and the run() method will be run in that new thread.

As for the run() method: by default it calls the run() method of the runnable provided in the constructor. If none was passed it will do nothing. Of course, if you override it (like you did) you can do anything you want.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Phone Myat Kyaw
Ranch Hand

Joined: Dec 26, 2008
Posts: 31
thanks Rob Prime, thanks for your explanation.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between start() and run() of a thread instance