Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Phone Myat Kyaw
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Phone Myat Kyaw
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rob Prime, thanks for your explanation.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic