| Author |
Concept about Memory storage and thread
|
A Chavda
Greenhorn
Joined: Apr 15, 2008
Posts: 3
|
|
Hi All, I have to clear my concept and so please let me know if I am wrong anywhere. Memory storage - We have stack and heap. Object and instance variables are stored in heap and local variables are stored in stack. also the refrences to the objects are stored in stack. Thread concept - Say we have method and we have local variable x decalred inside the method. Thread 1 invoke the method and sets the value of variable x as 10 and thread2 invoke the method and sets the variable x as 12. Question: Will we have two stack for two threads having variable x in both with value 10 and 12 correspondingly. I mean one stack will have x=10 and other stack will have x=12 Answer for this will help me a lot in understanding the concept deeper. Thanks in advance A chavda
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
|
Yes, each thread has its own completely different stack. Every time a method is called -- on the same thread, or on different threads -- space is reserved for a new set of local variables which is completely independent of any other set.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Memory storage - We have stack and heap.
Yes.
Object and instance variables are stored in heap and local variables are stored in stack.
Well... yeah. But instance variables are part of objects, so it is sufficient to say "Objects are stored in heap". Now... unfortunately, there is a monkey wrench. Java 6 changes this somewhat with "escape analysis" -- sometimes objects are on the stack.
also the refrences to the objects are stored in stack.
Well, a reference can be a instance variable, or it can be a local variable. Doesn't this statement partially conflict with your previous statement? Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: Concept about Memory storage and thread
|
|
|