Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads share the memory space  RSS feed

 
carox kaur
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a confusion in the topic of threading ....
Threads share the same process and memory space. With this, I think we mean that the different threads share the local variables as well as instance variables. I dont know whether I am right or not. I have made a small program:

Two threads are made and started under the main thread. Suppose first thread gets the chance to run, it prints the value of x and y: x=0,y=0. After that it incremented both the values to x=1, y=1. Then it goes to sleep. Mean time (suppose) main thread ran and created the second thread. It got the chance to run and prints the output:
x=0, y=0;
This is the point of confusion to me. When the first thread has already incremented the values of both the variables and when it is said that the threads share the memory space then how second thread has its own set of variables. It should print the values : x=1, y=1.
I think I am missing the very important part of threading. I would like to know the meaning of sharing the address spae/ memory etc between the threads. If this program is not accurate then I would like to know the correct program.
 
Anadi Misra
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the point of confusion to me. When the first thread has already incremented the values of both the variables and when it is said that the threads share the memory space then how second thread has its own set of variables.


The variable int x isn't really a shared resource. Change it to



and see the magic ;-)
 
carox kaur
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about local variable y? This means that local variables are not shared. Every thread of same instance will have own set of local and instance variables? These are not shared by the threads? Am I right?
 
R van Vliet
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
carox kaur wrote:And what about local variable y? This means that local variables are not shared. Every thread of same instance will have own set of local and instance variables? These are not shared by the threads? Am I right?

Yes, variables declared inside a method are method local and are instantiated each time the method is invocated and reaches the variable declaration.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strictly speaking changing x to static int x; is unpredictable and the threads could behave as if they had their own copies in some circumstances ...

i.e. happens before ordering issues, x++ isn't atomic etc etc so you should use a synchronized block or AtomicInteger or the like.

 
Henry Wong
author
Sheriff
Posts: 22866
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
carox kaur wrote:And what about local variable y? This means that local variables are not shared. Every thread of same instance will have own set of local and instance variables? These are not shared by the threads? Am I right?


Threads are part of the same process, hence, they share the same memory space. And any thread, can in theory, access any memory space of the process.

However, Java, or more specifically, the JVM, doesn't allow the threads to access each other's stacks (ie. local variables). You could however, use JNI, to allow one thread to access the local variables of another thread, but... that would really be not a good idea.

Henry
 
paresh vernekar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't look like the program written is the right way to demonstrate what you are trying to test. The main program is creating two thread instances.Each thread instance has its own instance variables. And hence, you are not seeing the expected result w.r.t instance variable x.

You could create another class TestThread with instance variable x and pass the same instance it to each of the threads. Do the operations on that instance of TestThread and you would see your queries answered

Thanks,
Paresh

 
James Ward
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you have created TWO NEW INSTANCES of thread1 class, there is no question of anything being shared between them.

IF, you make a attribute static, then it becomes common and available across instances of that class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!