aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt about threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt about threads" Watch "Doubt about threads" New topic
Author

Doubt about threads

Srishti Deepa
Greenhorn

Joined: Jun 14, 2006
Posts: 9
I am new to threads. When run() method is called, will it be executed in a new call stack or in the current call stack. I have a confusion. Please clarify.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

run() as any other Java method, when executes gets a new stack frame, but its parent thread would be the same for any number of executions.

HTH


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

If you directly call the run() method (not the start() method of the Thread object), it will be called just like any other normal method. In fact, the run() method is just a normal method just like anything else.

If you call the start() method to start a new thread, the run() method will be called by the system in a new thread.

Each thread has its own call stack, so in the second case, the run() method will be called in a new call stack.

(Don't confuse the terms "call stack" and "stack frame" - a stack frame is one set of parameters and return information on a call stack).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Jesper Young wrote:
Each thread has its own call stack, so in the second case, the run() method will be called in a new call stack.

So it means, each Runnable#run() method gets its own call stack ?
Jesper Young wrote:
(Don't confuse the terms "call stack" and "stack frame" - a stack frame is one set of parameters and return information on a call stack).

Ok, I get the difference.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Sagar Rohankar wrote:So it means, each Runnable#run() method gets its own call stack ?

Each thread has its own call stack. So if you create a new thread, a new call stack will also be created, and the stack frame for the run() method will be one of the first frames on that new call stack.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Thanks, got it now.
Srishti Deepa
Greenhorn

Joined: Jun 14, 2006
Posts: 9
Thanks so much for the info. I have got it now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Doubt about threads