Forums Register Login

Doubt about threads

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Thanks, got it now.
+Pie Number of slices to send: Send
Thanks so much for the info. I have got it now.
Hey cool! They got a blimp! But I have a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 869 times.
Similar Threads
regarding thread run method
multiple threads
Threads...
Threads and a local variable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:35:22.