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 ?
-
4For 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– Peter Smith2012-07-31 04:52:22 +00:00Commented Jul 31, 2012 at 4:52
-
1Agree with Peter Smith. It might take time, but afterwards you get perfect knowledge of what, how and why is going onsuperM– superM2012-07-31 08:05:05 +00:00Commented Jul 31, 2012 at 8:05
1 Answer
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– rwong2012-07-31 05:49:26 +00:00Commented Jul 31, 2012 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– Michael Borgwardt2012-07-31 07:31:16 +00:00Commented Jul 31, 2012 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– Geek2012-07-31 08:17:18 +00:00Commented Jul 31, 2012 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– Michael Borgwardt2012-07-31 11:32:41 +00:00Commented Jul 31, 2012 at 11:32
-
@MichaelBorgwardt I see what you are saying.Thanks for the clarification.Geek– Geek2012-07-31 11:41:52 +00:00Commented Jul 31, 2012 at 11:41