I have been reading Java Concurrency in Practice by Brian Goetz and inside the section Stack Confinement it is mentioned that each thread gets its own stack and so local variables are intrinsically confined to the executing thread; they exist on the executing threads stack, which is not accessible to other threads. What does he mean that each thread has its own execution stack ?

  • 4
    For deeper understanding on this, look up the stack pointer register and how it is used in assembly. This will explain how threads can easily have their own stack. – Peter Smith Jul 31 '12 at 4:52
  • 1
    Agree with Peter Smith. It might take time, but afterwards you get perfect knowledge of what, how and why is going on – superM Jul 31 '12 at 8:05

You know when you break to the debugger for whatever reason, and the IDE gives you a stack trace? And each method (stack frame) has its own set of local variables that you can examine in the debugger?

That's the "execution stack" of your program. It shows what the local state of your program looks like at the moment. What the author is saying is that each thread gets its own distinct execution stack like that. It has its own call stack, and each of the methods have their own local variables.

Because the variables are stored as part of the execution stack and not in the heap, they are unique to the thread that's being run and can't be shared directly. You can copy them, or pass references to objects to other threads in various ways, though, so that's mostly an academic distinction.

  • Please add an even stronger emphasis to your third paragraph to state that local variables that are exposed to other threads or long-living objects will no longer be confined. Thus, the claim quoted from OP's book is very dubious. – rwong Jul 31 '12 at 5:49
  • 4
    @rwong: It is impossible to expose a local variable to another thread iin a way that would be problematic. Variables only contain primitives or references. Objects live on the heap. – Michael Borgwardt Jul 31 '12 at 7:31
  • @MichaelBorgwardt Lets take two cases separately say the variable contains a primitive which is case A and say the variable contains a reference which is case B . Since primitives are passed by value , the local variables are indeed thread safe but what about references . They can be passed around ? Why do you say it is impossible ? – Geek Jul 31 '12 at 8:17
  • 2
    @Geek: Because the variable and the object it refers to are two separate and different things that should not be confused when talking at this level of detail. A local variable cannot be exposed to a different thread. Objects certainly can. – Michael Borgwardt Jul 31 '12 at 11:32
  • @MichaelBorgwardt I see what you are saying.Thanks for the clarification. – Geek Jul 31 '12 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.