This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about threads

 
Srishti Deepa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
 
Srishti Deepa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the info. I have got it now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic