• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads - Run and Start methods.

 
vikram Kesav
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i read from K&B is 'start' method of thread creates new stack for the thread.

i would like to know what is the meaning of stack for thread.
How this stack works....???where it resides in the memory...???
how run method get reference of new stack..???

Thanks,
Kesav
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kesav every thread has a method call stack of it's own. When you call start on a thread object, then a new thread is created and the run method is called on the new thread. The run method doesn't receive the stack. It is used by the thread to keep a list of pending method calls.

Suppose in a thread run is called then the method stack of the thread contains



Now if run calls another method func, then the stack of the thread becomes



Now if func calls a method myMethod, then the stack of the thread becomes



Now when the myMethod returns, the stack of the thread becomes



and when the func method returns, the stack of the thread becomes



And when the run method ends, then there is no pending method in the stack of the thread so the thread is ended.
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice pictorial explanation !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic