Using multithreading it is said that the threads shares same memory space of a process..
but if we are having a class that implements Multithreading, lets have an example:
then if we want to use multithreading then we have to create two inistances of the class..
As we are having two instances as two threads.
My question is when we create an instance of a class then one copy of all variables and methods created for that instance.
so in case of these two threads a copy will exist for all instances..
so how the process memory is shared???
as in above case
variable will exist for both the instances(Threads)
So where is the sharing???
It's simple. The process starts with, let's say, 300 megabytes of memory.
You create some objects. The memory for those objects comes from that 300 megabytes. Then you create some more objects. The memory for those objects also comes from that 300 megabytes. And so it goes, that 300 megabytes is shared among all the objects created by that process.
Did you think that when it said that two threads "shared" some memory, that they both got the same memory? That's not what "share" means. When you share a book with your friend, only one of you has the book at any time. Same with memory.