user3292785
user3292785

Reputation: 11

different stack for different thread

I was reading one of the question of multi threading and this question came into my mind,now we know that when one thread need to start an instance of thread class is made, then after we call start method,run method is loaded in class area, and then the other method are called so my question is,

when one particular thread is not finishes,and the other thread is called ,its run method is loaded,whether it is loaded into the same class area?

now what's the benefit of different stack for different threads?

and can a same method be called using different thread concurrently?if yes,how it is possible?

Upvotes: 0

Views: 102

Answers (4)

Stephen C
Stephen C

Reputation: 719709

Lets start with this.

... after we call start method, run method is loaded in class area, and then the other method are called ...

Your description is garbled, to the extent that I don't understand what you think is happening.

What actually happens is as follows ... assuming that you are using a Thread not a subclass of Thread.

  1. The parent thread creates the Thread instance, supplying a Runnable object as a constructor parameter. The Runnable is saved in a private variable of the Thread object.

  2. The parent thread calls start() on the new Thread object.

  3. The JVM allocates a new thread stack, and requests the OS (typically) to create and start a new native thread using that stack.

  4. The newly created child thread starts by calling Thread.run(), which in turn calls the run() method of the Runnable. These calls happen (naturally) using the new thread stack that we previously allocated.

  5. "Concurrently", the parent thread returns from the start() call, and continues running, independently of the child thread.


Now to your questions.

when one particular thread is not finishes,and the other thread is called ,its run method is loaded,whether it is loaded into the same class area?

This doesn't make sense to me. Your phrase "loaded into the same class area" does not align with what actually happens.

now what's the benefit of different stack for different threads?

Stacks are used to hold local variables and parameters for method calls. They operate by "pushing" a frame (consisting of the locals for a given method call) onto the stack when you start a method call. Then, when the method call terminates, the frame is "popped" from the stack. This scheme allows methods to call other methods, possibly recursively, without one call trampling on the variables of another one.

If you have two threads executing "concurrently", they will independently execute a different sequence of calls to different methods. If you (hypothetically) put the frames onto one stack, then the two threads would interfere with each other with their respective stack push and pop operations.

and can a same method be called using different thread concurrently?if yes,how it is possible?

Yes ... because the different threads each have different stack to hold the respective variables. See above.

could you please explain how does a thread starts?

That is covered by my description above.


Note that I used the word concurrently in quotes above. I'm trying to highlight that it is not specified whether two actions / action sequences that happen "concurrently" happen at the same time. They might do (e.g. if you have multiple cores) ... or the actions could be interleaved in some fashion. The actual behaviour depends on your Java platform, and can vary from one run / execution to another.

Upvotes: 2

Marko Topolnik
Marko Topolnik

Reputation: 200296

run method is loaded in class area

when one particular thread is not finishes,and the other thread is called ,its run method is loaded,whether it is loaded into the same class area?

I believe you have a misunderstanding here: a Thread is either created:

  • with new Thread(Runnable)—here a different Runnable, with a different run method, can be used for each thread;
  • or a distinct subclass of Thread is created, where that subclass' run method can be different each time.

what's the benefit of different stack for different threads?

Each thread is an independent line of execution, therefore each needs its own call stack.

can a same method be called using different thread concurrently?

Yes, that's what multithreading is about.

if yes,how it is possible?

To get an answer to this, you would have to provide at least one good reason why you think it is not possible.

Upvotes: 1

Ozan Tabak
Ozan Tabak

Reputation: 672

Each thread has its own stack because each thread may contain its own variables, resources etc.

The same method can be called from different threads concurrently , because each thread has its own program counter, so when a context switch occurs, each thread knows which line they should be processing next. The only think one should consider if there are some shared variables/resources used in the called method, if so you should handle it.

Upvotes: 0

Tim B
Tim B

Reputation: 41210

Each thread gets its own stack, and because values are stored in that stack yes they can call the same method at the same time - unless that method uses member or otherwise shared variables.

Each thread needs to have its own stack as otherwise threads would constantly interfere with each other.

Upvotes: 0

Related Questions